showAdaptiveDateTimePicker function
- required BuildContext context,
- required DateTimeFieldPickerMode mode,
- DateTimeFieldPickerPlatform? pickerPlatform,
- DateTime? initialPickerDateTime,
- DateTime? firstDate,
- DateTime? lastDate,
- CupertinoDatePickerOptions cupertinoDatePickerOptions = const CupertinoDatePickerOptions(),
- MaterialDatePickerOptions materialDatePickerOptions = const MaterialDatePickerOptions(),
- MaterialTimePickerOptions materialTimePickerOptions = const MaterialTimePickerOptions(),
Displays an adaptive date and time picker based on the current platform.
On iOS and macOS, it shows a Cupertino-style picker. On other platforms, it shows a Material-style picker.
The context
parameter is required to provide the necessary context for
the picker. The mode
parameter specifies whether to show a date, time,
or both date and time picker. The initialPickerDateTime
parameter sets
the initial date and time to be displayed by the picker.
The pickerPlatform
parameter allows overriding the platform detection to
force a specific style of picker. If pickerPlatform
is not specified,
the platform is inferred from the current theme's platform.
The firstDate
and lastDate
parameters set the selectable date range
for the picker. If not provided, defaults are used.
The cupertinoDatePickerOptions
parameter allows customization of the
Cupertino date picker. The materialDatePickerOptions
and
materialTimePickerOptions
parameters allow customization of the Material
date and time pickers respectively.
Returns a Future<DateTime?>
that completes with the selected date and time
or null if the user cancels the picker.
final DateTime? selectedDateTime = await showAdaptiveDateTimePicker(
context,
mode: DateTimeFieldPickerMode.dateAndTime,
initialPickerDateTime: DateTime.now(),
firstDate: DateTime(2011),
lastDate: DateTime(2125),
);
Implementation
Future<DateTime?> showAdaptiveDateTimePicker({
required BuildContext context,
required DateTimeFieldPickerMode mode,
DateTimeFieldPickerPlatform? pickerPlatform,
DateTime? initialPickerDateTime,
DateTime? firstDate,
DateTime? lastDate,
CupertinoDatePickerOptions cupertinoDatePickerOptions =
const CupertinoDatePickerOptions(),
MaterialDatePickerOptions materialDatePickerOptions =
const MaterialDatePickerOptions(),
MaterialTimePickerOptions materialTimePickerOptions =
const MaterialTimePickerOptions(),
}) async {
final TargetPlatform platform =
pickerPlatform?.toTargetPlatform(context) ?? Theme.of(context).platform;
if (platform == TargetPlatform.iOS || platform == TargetPlatform.macOS) {
return showCupertinoDateTimePicker(
context: context,
mode: mode,
initialPickerDateTime: initialPickerDateTime,
firstDate: firstDate ?? kDefaultFirstSelectableDate,
lastDate: lastDate ?? kDefaultLastSelectableDate,
cupertinoDatePickerOptions: cupertinoDatePickerOptions,
);
}
return showMaterialDateTimePicker(
context: context,
mode: mode,
initialPickerDateTime: initialPickerDateTime,
firstDate: firstDate,
lastDate: lastDate,
materialDatePickerOptions: materialDatePickerOptions,
materialTimePickerOptions: materialTimePickerOptions,
);
}