showMaterialDateTimePicker function

Future<DateTime?> showMaterialDateTimePicker({
  1. required BuildContext context,
  2. required DateTimeFieldPickerMode mode,
  3. DateTime? initialPickerDateTime,
  4. DateTime? firstDate,
  5. DateTime? lastDate,
  6. MaterialDatePickerOptions materialDatePickerOptions = const MaterialDatePickerOptions(),
  7. MaterialTimePickerOptions materialTimePickerOptions = const MaterialTimePickerOptions(),
})

Displays a Material-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 materialDatePickerOptions and materialTimePickerOptions parameters allow customization of the Material date and time pickers respectively.

final DateTime? selectedDateTime = await showMaterialDateTimePicker(
  context,
  mode: DateTimeFieldPickerMode.dateAndTime,
  initialPickerDateTime: DateTime.now(),
  firstDate: DateTime(2011),
  lastDate: DateTime(2125),
);

Implementation

Future<DateTime?> showMaterialDateTimePicker({
  required BuildContext context,
  required DateTimeFieldPickerMode mode,
  DateTime? initialPickerDateTime,
  DateTime? firstDate,
  DateTime? lastDate,
  MaterialDatePickerOptions materialDatePickerOptions =
      const MaterialDatePickerOptions(),
  MaterialTimePickerOptions materialTimePickerOptions =
      const MaterialTimePickerOptions(),
}) async {
  DateTime? selectedDateTime = initialPickerDateTime ?? DateTime.now();

  if (mode != DateTimeFieldPickerMode.time) {
    selectedDateTime = _getInitialDate(
      initialPickerDateTime,
      firstDate ?? kDefaultFirstSelectableDate,
      lastDate ?? kDefaultLastSelectableDate,
    );

    final DateTime? newDate = await _showMaterialDatePicker(
      context: context,
      initialPickerDateTime: selectedDateTime,
      firstDate: firstDate ?? kDefaultFirstSelectableDate,
      lastDate: lastDate ?? kDefaultLastSelectableDate,
      materialDatePickerOptions: materialDatePickerOptions,
    );

    if (newDate == null) {
      return null;
    }

    selectedDateTime = newDate;
  }

  if (mode != DateTimeFieldPickerMode.date) {
    final TimeOfDay? selectedTime = await _showMaterialTimePicker(
      context: context,
      initialPickerDateTime: _getInitialTime(
        initialPickerDateTime,
        firstDate ?? kDefaultFirstSelectableDate,
        lastDate ?? kDefaultLastSelectableDate,
      ),
      materialTimePickerOptions: materialTimePickerOptions,
    );

    if (selectedTime == null) {
      return null;
    }

    selectedDateTime = DateTime(
      selectedDateTime.year,
      selectedDateTime.month,
      selectedDateTime.day,
      selectedTime.hour,
      selectedTime.minute,
    );
  }

  return selectedDateTime;
}