showDatePicker static method

void showDatePicker(
  1. BuildContext context, {
  2. DateTime? minDateTime,
  3. DateTime? maxDateTime,
  4. DateTime? initialDateTime,
  5. String? dateFormat,
  6. DateTimePickerLocale locale = DATETIME_PICKER_LOCALE_DEFAULT,
  7. DateTimePickerMode pickerMode = DateTimePickerMode.date,
  8. DateTimePickerTheme pickerTheme = DateTimePickerTheme.Default,
  9. DateVoidCallback? onCancel,
  10. DateVoidCallback? onClose,
  11. DateValueCallback? onChange,
  12. DateValueCallback? onConfirm,
  13. int minuteDivider = 1,
  14. bool onMonthChangeStartWithFirstDate = false,
})

Display date picker in bottom sheet.

context: BuildContext minDateTime: DateTime minimum date time maxDateTime: DateTime maximum date time initialDateTime: DateTime initial date time for selected dateFormat: String date format pattern locale: DateTimePickerLocale internationalization pickerMode: DateTimePickerMode display mode: date(DatePicker)、time(TimePicker)、datetime(DateTimePicker) pickerTheme: DateTimePickerTheme the theme of date time picker onCancel: DateVoidCallback pressed title cancel widget event onClose: DateVoidCallback date picker closed event onChange: DateValueCallback selected date time changed event onConfirm: DateValueCallback pressed title confirm widget event

Implementation

static void showDatePicker(
  BuildContext context, {
  DateTime? minDateTime,
  DateTime? maxDateTime,
  DateTime? initialDateTime,
  String? dateFormat,
  DateTimePickerLocale locale: DATETIME_PICKER_LOCALE_DEFAULT,
  DateTimePickerMode pickerMode: DateTimePickerMode.date,
  DateTimePickerTheme pickerTheme: DateTimePickerTheme.Default,
  DateVoidCallback? onCancel,
  DateVoidCallback? onClose,
  DateValueCallback? onChange,
  DateValueCallback? onConfirm,
  int minuteDivider = 1,
  bool onMonthChangeStartWithFirstDate = false,
}) {
  // handle the range of datetime
  if (minDateTime == null) {
    minDateTime = DateTime.parse(DATE_PICKER_MIN_DATETIME);
  }
  if (maxDateTime == null) {
    maxDateTime = DateTime.parse(DATE_PICKER_MAX_DATETIME);
  }

  // handle initial DateTime
  if (initialDateTime == null) {
    initialDateTime = DateTime.now();
  }

  // Set value of date format
  if (dateFormat != null && dateFormat.length > 0) {
    // Check whether date format is legal or not
    if (DateTimeFormatter.isDayFormat(dateFormat)) {
      if (pickerMode == DateTimePickerMode.time) {
        pickerMode = DateTimeFormatter.isTimeFormat(dateFormat)
            ? DateTimePickerMode.datetime
            : DateTimePickerMode.date;
      }
    } else {
      if (pickerMode == DateTimePickerMode.date ||
          pickerMode == DateTimePickerMode.datetime) {
        pickerMode = DateTimePickerMode.time;
      }
    }
  } else {
    dateFormat = DateTimeFormatter.generateDateFormat(pickerMode);
  }

  Navigator.push(
    context,
    new _DatePickerRoute(
      onMonthChangeStartWithFirstDate: onMonthChangeStartWithFirstDate,
      minDateTime: minDateTime,
      maxDateTime: maxDateTime,
      initialDateTime: initialDateTime,
      dateFormat: dateFormat,
      locale: locale,
      pickerMode: pickerMode,
      pickerTheme: pickerTheme,
      onCancel: onCancel,
      onChange: onChange,
      onConfirm: onConfirm,
      theme: Theme.of(context),
      barrierLabel:
          MaterialLocalizations.of(context).modalBarrierDismissLabel,
      minuteDivider: minuteDivider,
    ),
  ).whenComplete(onClose ?? () => {});
}