From c8214efdb9356620cabf24e25715ff28891511dd Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Sat, 16 Dec 2023 18:52:38 +0100 Subject: [PATCH] Add controller support --- lib/src/field.dart | 21 +++++++-------------- lib/src/form_field.dart | 25 ++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/lib/src/field.dart b/lib/src/field.dart index aa8c6b9..28dff95 100644 --- a/lib/src/field.dart +++ b/lib/src/field.dart @@ -133,23 +133,20 @@ class DateTimeField extends StatelessWidget { } if (Theme.of(context).platform == TargetPlatform.iOS) { - final DateTime? _selectedDateTime = - await showCupertinoPicker(context, initialDateTime); + final DateTime? _selectedDateTime = await showCupertinoPicker(context, initialDateTime); if (_selectedDateTime != null) { onDateSelected!(_selectedDateTime); } } else { DateTime _selectedDateTime = initialDateTime; - const List modesWithDate = - [ + const List modesWithDate = [ DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode.date ]; if (modesWithDate.contains(mode)) { - final DateTime? _selectedDate = - await showMaterialDatePicker(context, initialDateTime); + final DateTime? _selectedDate = await showMaterialDatePicker(context, initialDateTime); if (_selectedDate != null) { _selectedDateTime = _selectedDate; @@ -158,15 +155,13 @@ class DateTimeField extends StatelessWidget { } } - final List modesWithTime = - [ + final List modesWithTime = [ DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode.time ]; if (modesWithTime.contains(mode)) { - final TimeOfDay? _selectedTime = await showMaterialTimePicker( - context, initialDateTime, + final TimeOfDay? _selectedTime = await showMaterialTimePicker(context, initialDateTime, initialEntryMode: initialTimePickerEntryMode); if (_selectedTime != null) { @@ -186,8 +181,7 @@ class DateTimeField extends StatelessWidget { /// Launches the Material time picker by invoking [showTimePicker]. /// Can be @[override]n to allow further customization of the picker options - Future showMaterialTimePicker( - BuildContext context, DateTime initialDateTime, + Future showMaterialTimePicker(BuildContext context, DateTime initialDateTime, {TimePickerEntryMode initialEntryMode = TimePickerEntryMode.dial}) async { return showTimePicker( initialTime: TimeOfDay.fromDateTime(initialDateTime), @@ -274,8 +268,7 @@ enum DateTimeFieldPickerMode { time, date, dateAndTime } /// Returns the [CupertinoDatePickerMode] corresponding to the selected /// [DateTimeFieldPickerMode]. This exists to prevent redundancy in the [DateTimeField] /// widget parameters. -CupertinoDatePickerMode cupertinoModeFromPickerMode( - DateTimeFieldPickerMode mode) { +CupertinoDatePickerMode cupertinoModeFromPickerMode(DateTimeFieldPickerMode mode) { switch (mode) { case DateTimeFieldPickerMode.time: return CupertinoDatePickerMode.time; diff --git a/lib/src/form_field.dart b/lib/src/form_field.dart index 93770d8..71168cc 100644 --- a/lib/src/form_field.dart +++ b/lib/src/form_field.dart @@ -31,27 +31,37 @@ class DateTimeFormField extends FormField { DatePickerMode initialDatePickerMode = DatePickerMode.day, DateTimeFieldPickerMode mode = DateTimeFieldPickerMode.dateAndTime, TimePickerEntryMode initialTimePickerEntryMode = TimePickerEntryMode.dial, + DateTimeFormFieldController? controller, DateTimeFieldCreator fieldCreator = DateTimeField.new, }) : super( key: key, - initialValue: initialValue, + initialValue: + initialValue == null && controller != null ? controller.selectedDate : initialValue, onSaved: onSaved, validator: validator, autovalidateMode: autovalidateMode, enabled: enabled, builder: (FormFieldState field) { + if (controller != null) { + controller.addListener(() { + field.didChange(controller.selectedDate); + }); + } + // Theme defaults are applied inside the _InputDropdown widget final InputDecoration _decorationWithThemeDefaults = decoration ?? const InputDecoration(); final InputDecoration effectiveDecoration = - _decorationWithThemeDefaults.copyWith( - errorText: field.errorText); + _decorationWithThemeDefaults.copyWith(errorText: field.errorText); void onChangedHandler(DateTime value) { if (onDateSelected != null) { onDateSelected(value); } + if (controller != null) { + controller.selectedDate = value; + } field.didChange(value); } @@ -77,3 +87,12 @@ class DateTimeFormField extends FormField { @override FormFieldState createState() => FormFieldState(); } + +class DateTimeFormFieldController extends ChangeNotifier { + DateTime? _selectedDate; + DateTime? get selectedDate => _selectedDate; + set selectedDate(DateTime? value) { + _selectedDate = value; + notifyListeners(); + } +}