showAdaptiveDateTimePicker function

Future<DateTime?> showAdaptiveDateTimePicker({
  1. required BuildContext context,
  2. required DateTimeFieldPickerMode mode,
  3. DateTimeFieldPickerPlatform? pickerPlatform,
  4. DateTime? initialPickerDateTime,
  5. DateTime? firstDate,
  6. DateTime? lastDate,
  7. CupertinoDatePickerOptions cupertinoDatePickerOptions = const CupertinoDatePickerOptions(),
  8. MaterialDatePickerOptions materialDatePickerOptions = const MaterialDatePickerOptions(),
  9. 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,
  );
}