showCupertinoDateTimePicker function

Future<DateTime?> showCupertinoDateTimePicker({
  1. required BuildContext context,
  2. required DateTimeFieldPickerMode mode,
  3. DateTime? initialPickerDateTime,
  4. DateTime? firstDate,
  5. DateTime? lastDate,
  6. CupertinoDatePickerOptions cupertinoDatePickerOptions = const CupertinoDatePickerOptions(),
})

Displays a Cupertino-style date and time picker in a modal popup.

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 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.

Returns a Future<DateTime?> that completes with the selected date and time or null if the user cancels the picker.

final DateTime? selectedDateTime = await showCupertinoDateTimePicker(
 context,
 mode: DateTimeFieldPickerMode.dateAndTime,
 initialPickerDateTime: DateTime.now(),
 firstDate: DateTime(2011),
 lastDate: DateTime(2125),
 cupertinoDatePickerOptions: CupertinoDatePickerOptions(
 minuteInterval: 2,
 ),
 );

Implementation

Future<DateTime?> showCupertinoDateTimePicker({
  required BuildContext context,
  required DateTimeFieldPickerMode mode,
  DateTime? initialPickerDateTime,
  DateTime? firstDate,
  DateTime? lastDate,
  CupertinoDatePickerOptions cupertinoDatePickerOptions =
      const CupertinoDatePickerOptions(),
}) async {
  return showCupertinoModalPopup<DateTime?>(
    useRootNavigator: cupertinoDatePickerOptions.useRootNavigator,
    context: context,
    builder: (BuildContext context) {
      return CupertinoDatePickerModalSheet(
        initialPickerDateTime: initialPickerDateTime ?? DateTime.now(),
        options: cupertinoDatePickerOptions,
        use24hFormat: detect24HourFormat(context),
        firstDate: firstDate ?? kDefaultFirstSelectableDate,
        lastDate: lastDate ?? kDefaultLastSelectableDate,
        mode: mode,
      );
    },
  );
}