Skip to content

Commit 40bdb32

Browse files
feat: add allowClear and clearIcon to FormBuilderDateTimePicker
1 parent bff073a commit 40bdb32

4 files changed

Lines changed: 45 additions & 18 deletions

File tree

example/lib/sources/complete_form.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,17 @@ class _CompleteFormState extends State<CompleteForm> {
5555
inputType: InputType.both,
5656
decoration: InputDecoration(
5757
labelText: 'Appointment Time',
58-
suffixIcon: IconButton(
59-
icon: const Icon(Icons.close),
60-
onPressed: () {
61-
_formKey.currentState!.fields['date']?.didChange(null);
62-
},
63-
),
58+
// TODO: use allowClear property to automatically perform this action.
59+
// suffixIcon: IconButton(
60+
// icon: const Icon(Icons.close),
61+
// onPressed: () {
62+
// _formKey.currentState!.fields['date']?.didChange(null);
63+
// },
64+
// ),
6465
),
6566
initialTime: const TimeOfDay(hour: 8, minute: 0),
6667
// locale: const Locale.fromSubtags(languageCode: 'fr'),
68+
allowClear: true,
6769
),
6870
FormBuilderDateRangePicker(
6971
name: 'date_range',

example/pubspec.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ packages:
6060
path: ".."
6161
relative: true
6262
source: path
63-
version: "10.3.0+1"
63+
version: "10.3.0+2"
6464
flutter_lints:
6565
dependency: "direct dev"
6666
description:
@@ -131,10 +131,10 @@ packages:
131131
dependency: transitive
132132
description:
133133
name: matcher
134-
sha256: "12956d0ad8390bbcc63ca2e1469c0619946ccb52809807067a7020d57e647aa6"
134+
sha256: dc0b7dc7651697ea4ff3e69ef44b0407ea32c487a39fff6a4004fa585e901861
135135
url: "https://pub.dev"
136136
source: hosted
137-
version: "0.12.18"
137+
version: "0.12.19"
138138
material_color_utilities:
139139
dependency: transitive
140140
description:
@@ -208,10 +208,10 @@ packages:
208208
dependency: transitive
209209
description:
210210
name: test_api
211-
sha256: "93167629bfc610f71560ab9312acdda4959de4df6fac7492c89ff0d3886f6636"
211+
sha256: "8161c84903fd860b26bfdefb7963b3f0b68fee7adea0f59ef805ecca346f0c7a"
212212
url: "https://pub.dev"
213213
source: hosted
214-
version: "0.7.9"
214+
version: "0.7.10"
215215
vector_math:
216216
dependency: transitive
217217
description:

lib/src/fields/form_builder_date_time_picker.dart

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import 'dart:ui' as ui;
22

33
import 'package:flutter/material.dart';
44
import 'package:flutter/services.dart';
5-
6-
import 'package:intl/intl.dart';
7-
85
import 'package:flutter_form_builder/flutter_form_builder.dart';
6+
import 'package:intl/intl.dart';
97

108
enum InputType { date, time, both }
119

@@ -123,6 +121,9 @@ class FormBuilderDateTimePicker extends FormBuilderFieldDecoration<DateTime> {
123121
final EntryModeChangeCallback? onEntryModeChanged;
124122
final bool barrierDismissible;
125123

124+
final bool allowClear;
125+
final Widget? clearIcon;
126+
126127
/// If true, disables the picker so it's not shown when the field is tapped.
127128
final bool disablePicker;
128129

@@ -197,6 +198,9 @@ class FormBuilderDateTimePicker extends FormBuilderFieldDecoration<DateTime> {
197198
this.onEntryModeChanged,
198199
this.disablePicker = false,
199200
this.barrierDismissible = true,
201+
// TODO: set allClear to true if that's the default behaviour
202+
this.allowClear = false,
203+
this.clearIcon,
200204
}) : super(
201205
builder: (FormFieldState<DateTime?> field) {
202206
final state = field as _FormBuilderDateTimePickerState;
@@ -404,6 +408,27 @@ class _FormBuilderDateTimePickerState
404408
DateTime? convert(TimeOfDay? time) =>
405409
time == null ? null : DateTime(1, 1, 1, time.hour, time.minute);
406410

411+
/// Sets the [allowClear] property for automatic DateTime reset, and [clearIcon] to a default or user defined icon.
412+
@override
413+
InputDecoration get decoration => widget.allowClear
414+
? super.decoration.copyWith(
415+
suffix: value == null
416+
? null
417+
: IconButton(
418+
padding: EdgeInsets.zero,
419+
constraints: const BoxConstraints(
420+
maxWidth: 24,
421+
maxHeight: 24,
422+
),
423+
onPressed: () {
424+
focus();
425+
didChange(null);
426+
},
427+
icon: widget.clearIcon ?? const Icon(Icons.clear),
428+
),
429+
)
430+
: super.decoration;
431+
407432
@override
408433
void didChange(DateTime? value) {
409434
super.didChange(value);

pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ packages:
111111
dependency: transitive
112112
description:
113113
name: matcher
114-
sha256: "12956d0ad8390bbcc63ca2e1469c0619946ccb52809807067a7020d57e647aa6"
114+
sha256: dc0b7dc7651697ea4ff3e69ef44b0407ea32c487a39fff6a4004fa585e901861
115115
url: "https://pub.dev"
116116
source: hosted
117-
version: "0.12.18"
117+
version: "0.12.19"
118118
material_color_utilities:
119119
dependency: transitive
120120
description:
@@ -188,10 +188,10 @@ packages:
188188
dependency: transitive
189189
description:
190190
name: test_api
191-
sha256: "93167629bfc610f71560ab9312acdda4959de4df6fac7492c89ff0d3886f6636"
191+
sha256: "8161c84903fd860b26bfdefb7963b3f0b68fee7adea0f59ef805ecca346f0c7a"
192192
url: "https://pub.dev"
193193
source: hosted
194-
version: "0.7.9"
194+
version: "0.7.10"
195195
vector_math:
196196
dependency: transitive
197197
description:

0 commit comments

Comments
 (0)