showCustomDatePicker function

Future<DateTime?> showCustomDatePicker({
  1. required BuildContext context,
  2. DatePickerType type = DatePickerType.spin,
  3. DateTime? initialDate,
  4. DateTime? firstDate,
  5. DateTime? lastDate,
  6. CustomDatePickerThemeData theme = CustomDatePickerThemeData.minimal,
})

Opens a spin, calendar, or dropdown picker. The result is one calendar day.

Implementation

Future<DateTime?> showCustomDatePicker({
  required BuildContext context,
  DatePickerType type = DatePickerType.spin,
  DateTime? initialDate,
  DateTime? firstDate,
  DateTime? lastDate,
  CustomDatePickerThemeData theme = CustomDatePickerThemeData.minimal,
}) {
  switch (type) {
    case DatePickerType.spin:
    case DatePickerType.calendar:
    case DatePickerType.dropdown:
      break;
    case DatePickerType.input:
    case DatePickerType.range:
      throw ArgumentError(
        '$type is not opened with showCustomDatePicker.',
      );
  }

  final engine = SelectionEngine(firstDate: firstDate, lastDate: lastDate);
  final initial = engine.normalize(initialDate ?? DateTime.now());

  return showModalBottomSheet<DateTime>(
    context: context,
    isScrollControlled: true,
    backgroundColor: Colors.transparent,
    builder: (context) {
      return CustomDatePickerTheme(
        data: theme,
        child: switch (type) {
          DatePickerType.spin => SpinDatePicker(
            engine: engine,
            initialDate: initial,
          ),
          DatePickerType.calendar => CalendarPickerView(
            engine: engine,
            initialDate: initial,
          ),
          DatePickerType.dropdown => DropdownDatePicker(
            engine: engine,
            initialDate: initial,
          ),
          DatePickerType.input || DatePickerType.range => const SizedBox.shrink(),
        },
      );
    },
  );
}