showMaterialDatePicker function

void 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. String? cancelText,
  12. double? maxLongSide,
  13. double? maxShortSide,
  14. ValueChanged<DateTime>? onChanged,
  15. VoidCallback? onConfirmed,
  16. VoidCallback? onCancelled,
})

Allows selection of a date.

Implementation

void showMaterialDatePicker({
  required BuildContext context,
  String? title,
  required DateTime firstDate,
  required DateTime lastDate,
  required DateTime selectedDate,
  Color? headerColor,
  Color? headerTextColor,
  Color? backgroundColor,
  Color? buttonTextColor,
  String? confirmText,
  String? cancelText,
  double? maxLongSide,
  double? maxShortSide,
  ValueChanged<DateTime>? onChanged,
  VoidCallback? onConfirmed,
  VoidCallback? onCancelled,
}) {
  showDialog<DateTime>(
    context: context,
    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,
            child: SingleChildScrollView(
              child: CalendarDatePicker(
                initialDate: selectedDate,
                firstDate: firstDate,
                lastDate: lastDate,
                onDateChanged: (date) => selectedDate = date,
              ),
            ),
            okPressed: () => Navigator.of(context).pop(selectedDate),
          );
        },
      );
    },
  ).then((selection) {
    if (onChanged != null && selection != null) onChanged(selection);
    if (onCancelled != null && selection == null) onCancelled();
    if (onConfirmed != null && selection != null) onConfirmed();
  });
}