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) {
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<DateTimeFieldPickerMode> modesWithDate =
<DateTimeFieldPickerMode>[
const List<DateTimeFieldPickerMode> modesWithDate = <DateTimeFieldPickerMode>[
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<DateTimeFieldPickerMode> modesWithTime =
<DateTimeFieldPickerMode>[
final List<DateTimeFieldPickerMode> modesWithTime = <DateTimeFieldPickerMode>[
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<TimeOfDay?> showMaterialTimePicker(
BuildContext context, DateTime initialDateTime,
Future<TimeOfDay?> 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;

View file

@ -31,27 +31,37 @@ class DateTimeFormField extends FormField<DateTime> {
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<DateTime> 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<DateTime> {
@override
FormFieldState<DateTime> createState() => FormFieldState<DateTime>();
}
class DateTimeFormFieldController extends ChangeNotifier {
DateTime? _selectedDate;
DateTime? get selectedDate => _selectedDate;
set selectedDate(DateTime? value) {
_selectedDate = value;
notifyListeners();
}
}