Add controller support

This commit is contained in:
Dominic Grimm 2023-12-16 18:52:38 +01:00
parent 190a5d734b
commit c8214efdb9
2 changed files with 29 additions and 17 deletions

View file

@ -133,23 +133,20 @@ class DateTimeField extends StatelessWidget {
} }
if (Theme.of(context).platform == TargetPlatform.iOS) { if (Theme.of(context).platform == TargetPlatform.iOS) {
final DateTime? _selectedDateTime = final DateTime? _selectedDateTime = await showCupertinoPicker(context, initialDateTime);
await showCupertinoPicker(context, initialDateTime);
if (_selectedDateTime != null) { if (_selectedDateTime != null) {
onDateSelected!(_selectedDateTime); onDateSelected!(_selectedDateTime);
} }
} else { } else {
DateTime _selectedDateTime = initialDateTime; DateTime _selectedDateTime = initialDateTime;
const List<DateTimeFieldPickerMode> modesWithDate = const List<DateTimeFieldPickerMode> modesWithDate = <DateTimeFieldPickerMode>[
<DateTimeFieldPickerMode>[
DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode.dateAndTime,
DateTimeFieldPickerMode.date DateTimeFieldPickerMode.date
]; ];
if (modesWithDate.contains(mode)) { if (modesWithDate.contains(mode)) {
final DateTime? _selectedDate = final DateTime? _selectedDate = await showMaterialDatePicker(context, initialDateTime);
await showMaterialDatePicker(context, initialDateTime);
if (_selectedDate != null) { if (_selectedDate != null) {
_selectedDateTime = _selectedDate; _selectedDateTime = _selectedDate;
@ -158,15 +155,13 @@ class DateTimeField extends StatelessWidget {
} }
} }
final List<DateTimeFieldPickerMode> modesWithTime = final List<DateTimeFieldPickerMode> modesWithTime = <DateTimeFieldPickerMode>[
<DateTimeFieldPickerMode>[
DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode.dateAndTime,
DateTimeFieldPickerMode.time DateTimeFieldPickerMode.time
]; ];
if (modesWithTime.contains(mode)) { if (modesWithTime.contains(mode)) {
final TimeOfDay? _selectedTime = await showMaterialTimePicker( final TimeOfDay? _selectedTime = await showMaterialTimePicker(context, initialDateTime,
context, initialDateTime,
initialEntryMode: initialTimePickerEntryMode); initialEntryMode: initialTimePickerEntryMode);
if (_selectedTime != null) { if (_selectedTime != null) {
@ -186,8 +181,7 @@ class DateTimeField extends StatelessWidget {
/// Launches the Material time picker by invoking [showTimePicker]. /// Launches the Material time picker by invoking [showTimePicker].
/// Can be @[override]n to allow further customization of the picker options /// Can be @[override]n to allow further customization of the picker options
Future<TimeOfDay?> showMaterialTimePicker( Future<TimeOfDay?> showMaterialTimePicker(BuildContext context, DateTime initialDateTime,
BuildContext context, DateTime initialDateTime,
{TimePickerEntryMode initialEntryMode = TimePickerEntryMode.dial}) async { {TimePickerEntryMode initialEntryMode = TimePickerEntryMode.dial}) async {
return showTimePicker( return showTimePicker(
initialTime: TimeOfDay.fromDateTime(initialDateTime), initialTime: TimeOfDay.fromDateTime(initialDateTime),
@ -274,8 +268,7 @@ enum DateTimeFieldPickerMode { time, date, dateAndTime }
/// Returns the [CupertinoDatePickerMode] corresponding to the selected /// Returns the [CupertinoDatePickerMode] corresponding to the selected
/// [DateTimeFieldPickerMode]. This exists to prevent redundancy in the [DateTimeField] /// [DateTimeFieldPickerMode]. This exists to prevent redundancy in the [DateTimeField]
/// widget parameters. /// widget parameters.
CupertinoDatePickerMode cupertinoModeFromPickerMode( CupertinoDatePickerMode cupertinoModeFromPickerMode(DateTimeFieldPickerMode mode) {
DateTimeFieldPickerMode mode) {
switch (mode) { switch (mode) {
case DateTimeFieldPickerMode.time: case DateTimeFieldPickerMode.time:
return CupertinoDatePickerMode.time; return CupertinoDatePickerMode.time;

View file

@ -31,27 +31,37 @@ class DateTimeFormField extends FormField<DateTime> {
DatePickerMode initialDatePickerMode = DatePickerMode.day, DatePickerMode initialDatePickerMode = DatePickerMode.day,
DateTimeFieldPickerMode mode = DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode mode = DateTimeFieldPickerMode.dateAndTime,
TimePickerEntryMode initialTimePickerEntryMode = TimePickerEntryMode.dial, TimePickerEntryMode initialTimePickerEntryMode = TimePickerEntryMode.dial,
DateTimeFormFieldController? controller,
DateTimeFieldCreator fieldCreator = DateTimeField.new, DateTimeFieldCreator fieldCreator = DateTimeField.new,
}) : super( }) : super(
key: key, key: key,
initialValue: initialValue, initialValue:
initialValue == null && controller != null ? controller.selectedDate : initialValue,
onSaved: onSaved, onSaved: onSaved,
validator: validator, validator: validator,
autovalidateMode: autovalidateMode, autovalidateMode: autovalidateMode,
enabled: enabled, enabled: enabled,
builder: (FormFieldState<DateTime> field) { builder: (FormFieldState<DateTime> field) {
if (controller != null) {
controller.addListener(() {
field.didChange(controller.selectedDate);
});
}
// Theme defaults are applied inside the _InputDropdown widget // Theme defaults are applied inside the _InputDropdown widget
final InputDecoration _decorationWithThemeDefaults = final InputDecoration _decorationWithThemeDefaults =
decoration ?? const InputDecoration(); decoration ?? const InputDecoration();
final InputDecoration effectiveDecoration = final InputDecoration effectiveDecoration =
_decorationWithThemeDefaults.copyWith( _decorationWithThemeDefaults.copyWith(errorText: field.errorText);
errorText: field.errorText);
void onChangedHandler(DateTime value) { void onChangedHandler(DateTime value) {
if (onDateSelected != null) { if (onDateSelected != null) {
onDateSelected(value); onDateSelected(value);
} }
if (controller != null) {
controller.selectedDate = value;
}
field.didChange(value); field.didChange(value);
} }
@ -77,3 +87,12 @@ class DateTimeFormField extends FormField<DateTime> {
@override @override
FormFieldState<DateTime> createState() => FormFieldState<DateTime>(); FormFieldState<DateTime> createState() => FormFieldState<DateTime>();
} }
class DateTimeFormFieldController extends ChangeNotifier {
DateTime? _selectedDate;
DateTime? get selectedDate => _selectedDate;
set selectedDate(DateTime? value) {
_selectedDate = value;
notifyListeners();
}
}