Feature: Extract CupertinoDatePicker into an Overrideable Method

This commit is contained in:
Alexander Scott 2022-02-26 12:06:00 +10:00
parent f9d16e3a0b
commit 6b01c427b8

View file

@ -5,7 +5,7 @@ import 'package:intl/intl.dart';
final DateTime _kDefaultFirstSelectableDate = DateTime(1900); final DateTime _kDefaultFirstSelectableDate = DateTime(1900);
final DateTime _kDefaultLastSelectableDate = DateTime(2100); final DateTime _kDefaultLastSelectableDate = DateTime(2100);
const double _kCupertinoDatePickerHeight = 216; const double kCupertinoDatePickerHeight = 216;
/// [DateTimeField] /// [DateTimeField]
/// ///
@ -106,22 +106,11 @@ class DateTimeField extends StatelessWidget {
} }
if (Theme.of(context).platform == TargetPlatform.iOS) { if (Theme.of(context).platform == TargetPlatform.iOS) {
showModalBottomSheet<void>( final DateTime? _selectedDateTime =
context: context, await showCupertinoPicker(context, initialDateTime);
builder: (BuildContext builder) { if (_selectedDateTime != null) {
return SizedBox( onDateSelected!(_selectedDateTime);
height: _kCupertinoDatePickerHeight, }
child: CupertinoDatePicker(
mode: _cupertinoModeFromPickerMode(mode),
onDateTimeChanged: onDateSelected!,
initialDateTime: initialDateTime,
minimumDate: firstDate,
maximumDate: lastDate,
use24hFormat: use24hFormat,
),
);
},
);
} else { } else {
DateTime _selectedDateTime = initialDateTime; DateTime _selectedDateTime = initialDateTime;
@ -195,6 +184,32 @@ class DateTimeField extends StatelessWidget {
); );
} }
/// Launches the [CupertinoDatePicker] within a [showModalBottomSheet].
/// Can be @[override]n to allow further customization of the picker options
Future<DateTime?> showCupertinoPicker(
BuildContext context,
DateTime initialDateTime,
) async {
DateTime? pickedDate;
await showModalBottomSheet<DateTime?>(
context: context,
builder: (BuildContext builder) {
return SizedBox(
height: kCupertinoDatePickerHeight,
child: CupertinoDatePicker(
mode: cupertinoModeFromPickerMode(mode),
onDateTimeChanged: (DateTime dt) => pickedDate = dt,
initialDateTime: initialDateTime,
minimumDate: firstDate,
maximumDate: lastDate,
use24hFormat: use24hFormat,
),
);
},
);
return pickedDate;
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
String? text; String? text;
@ -223,7 +238,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: