showMaterialDatePicker function

Future<DateTime?> showMaterialDatePicker({
  1. required BuildContext context,
  2. String? title,
  3. required DateTime firstDate,
  4. required DateTime lastDate,
  5. required DateTime selectedDate,
  6. Color? headerColor,
  7. Color? headerTextColor,
  8. Color? backgroundColor,
  9. Color? buttonTextColor,
  10. String? confirmText,
  11. bool cancellable = true,
  12. String? cancelText,
  13. double? maxLongSide,
  14. double? maxShortSide,
  15. ValueChanged<DateTime>? onChanged,
  16. VoidCallback? onConfirmed,
  17. VoidCallback? onCancelled,
})

Allows selection of a date.

Implementation

Future<DateTime?> showMaterialDatePicker({
  required BuildContext context,

  /// The title for the dialog box
  String? title,

  /// The intiial first date in the selection range
  required DateTime firstDate,

  /// The intiial last date in the selection range
  required DateTime lastDate,

  /// The initial single selected date
  required DateTime selectedDate,

  /// The dialog header color (overrides theme)
  Color? headerColor,

  /// The dialog header text color (overrides theme)
  Color? headerTextColor,

  /// The dialog background color (overrides theme)
  Color? backgroundColor,

  /// The button text color (overrides theme)
  Color? buttonTextColor,

  /// Text to display in the confirm button
  String? confirmText,

  /// Whether to display a cancel button
  bool cancellable = true,

  /// Text to display in the cancel button
  String? cancelText,

  /// Used to restrict how tall the dialog can be.
  double? maxLongSide,

  /// Used to restrict how wide the dialog can be.
  double? maxShortSide,

  /// Function that gets called when the value is changed
  ValueChanged<DateTime>? onChanged,

  /// Function that gets called when the confirm button is pressed
  VoidCallback? onConfirmed,

  /// Function that gets called when the cancel button is pressed
  VoidCallback? onCancelled,
}) {
  return showDialog<DateTime>(
    context: context,
    barrierDismissible: cancellable,
    builder: (BuildContext context) {
      return OrientationBuilder(
        builder: (context, orientation) {
          return ResponsiveDialog(
            context: context,
            title: title,
            headerColor: headerColor,
            headerTextColor: headerTextColor,
            backgroundColor: backgroundColor,
            buttonTextColor: buttonTextColor,
            confirmText: confirmText,
            cancelText: cancelText,
            maxLongSide: maxLongSide,
            maxShortSide: maxLongSide,
            cancelButtonVisible: cancellable,
            child: SingleChildScrollView(
              child: CalendarDatePicker(
                initialDate: selectedDate,
                firstDate: firstDate,
                lastDate: lastDate,
                onDateChanged: (date) => selectedDate = date,
              ),
            ),
            okPressed: () => Navigator.of(context).pop(selectedDate),
          );
        },
      );
    },
  ).then((selection) {
    if (selection != null) {
      onChanged?.call(selection);
      onConfirmed?.call();
    } else {
      onCancelled?.call();
    }
    return selection;
  });
}