showCustomDatePicker static method

Future<DateTime?> showCustomDatePicker({
  1. DateTime? initialDate,
  2. DateTime? firstDate,
  3. DateTime? lastDate,
  4. String? helpText,
  5. String? cancelText,
  6. String? confirmText,
  7. DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
  8. DatePickerMode initialDatePickerMode = DatePickerMode.day,
  9. String? fieldHintText,
  10. String? fieldLabelText,
  11. Color? primaryColor,
  12. Color? textColor,
  13. Color? backgroundColor,
  14. Color? headerBackgroundColor,
  15. bool barrierDismissible = true,
})

Implementation

static Future<DateTime?> showCustomDatePicker({
  DateTime? initialDate,
  DateTime? firstDate,
  DateTime? lastDate,
  String? helpText,
  String? cancelText,
  String? confirmText,
  DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
  DatePickerMode initialDatePickerMode = DatePickerMode.day,
  String? fieldHintText,
  String? fieldLabelText,
  Color? primaryColor,
  Color? textColor,
  Color? backgroundColor,
  Color? headerBackgroundColor,
  bool barrierDismissible = true,
}) async {
  initialDate ??= DateTime.now();
  firstDate ??= DateTime(1900);
  lastDate ??= DateTime(2100);
  primaryColor = AppColors.primary;
  backgroundColor ??= Colors.white;
  headerBackgroundColor ??= primaryColor;
  textColor ??= AppColors.text;

  final DateTime? picked = await showDatePicker(
    context: Get.context!,
    initialDate: initialDate,
    firstDate: firstDate,
    lastDate: lastDate,
    helpText: helpText,
    cancelText: cancelText,
    confirmText: confirmText,
    initialEntryMode: initialEntryMode,
    initialDatePickerMode: initialDatePickerMode,
    fieldHintText: fieldHintText,
    fieldLabelText: fieldLabelText,
    builder: (context, child) {
      return Theme(
        data: Theme.of(context).copyWith(
          colorScheme: ColorScheme.light(
            primary: primaryColor ?? AppColors.primary,
            onPrimary: Colors.white,
            onSurface: textColor ?? AppColors.text,
            surface: backgroundColor ?? Colors.white,
          ),
          dialogBackgroundColor: backgroundColor,
          textButtonTheme: TextButtonThemeData(
            style: TextButton.styleFrom(
              foregroundColor: primaryColor,
              textStyle: AppTextThemes.button(),
            ),
          ),
          datePickerTheme: DatePickerThemeData(
            backgroundColor: backgroundColor,
            headerBackgroundColor: headerBackgroundColor,
            headerForegroundColor: Colors.white,
            surfaceTintColor: backgroundColor,
            dayBackgroundColor: WidgetStateProperty.resolveWith((states) {
              if (states.contains(WidgetState.selected)) {
                return primaryColor;
              }
              return null;
            }),
            todayBackgroundColor: WidgetStateProperty.resolveWith((states) {
              if (states.contains(WidgetState.selected)) {
                return primaryColor;
              }
              return (primaryColor ?? AppColors.primary).withOpacity(0.2);
            }),
            dayForegroundColor: WidgetStateProperty.resolveWith((states) {
              if (states.contains(WidgetState.selected)) {
                return Colors.white;
              }
              return textColor;
            }),
            todayForegroundColor: WidgetStateProperty.resolveWith((states) {
              if (states.contains(WidgetState.selected)) {
                return Colors.white;
              }
              return primaryColor;
            }),
            yearBackgroundColor: WidgetStateProperty.resolveWith((states) {
              if (states.contains(WidgetState.selected)) {
                return primaryColor;
              }
              return null;
            }),
            yearForegroundColor: WidgetStateProperty.resolveWith((states) {
              if (states.contains(WidgetState.selected)) {
                return Colors.white;
              }
              return textColor;
            }),
            headerHeadlineStyle: AppTextThemes.heading5(color: Colors.white),
            headerHelpStyle:
                AppTextThemes.bodySmall(color: Colors.white.withOpacity(0.8)),
            yearStyle: AppTextThemes.bodyMedium(),
            dayStyle: AppTextThemes.bodyMedium(),
            weekdayStyle: AppTextThemes.caption(
                color: (textColor ?? AppColors.text).withOpacity(0.7)),
          ),
        ),
        child: child!,
      );
    },
    barrierDismissible: barrierDismissible,
  );

  return picked;
}