showSyncDatePicker function

dynamic showSyncDatePicker({
  1. DateTime? initialDate,
  2. required DateTime firstDate,
  3. required DateTime lastDate,
  4. DateTime? currentDate,
  5. DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
  6. SelectableDayPredicate? selectableDayPredicate,
  7. String? helpText,
  8. String? cancelText,
  9. String? confirmText,
  10. Locale? locale,
  11. bool barrierDismissible = true,
  12. Color? barrierColor,
  13. String? barrierLabel,
  14. bool useRootNavigator = true,
  15. RouteSettings? routeSettings,
  16. TextDirection? textDirection,
  17. TransitionBuilder? builder,
  18. DatePickerMode initialDatePickerMode = DatePickerMode.day,
  19. String? errorFormatText,
  20. String? errorInvalidText,
  21. String? fieldHintText,
  22. String? fieldLabelText,
  23. TextInputType? keyboardType,
  24. Offset? anchorPoint,
  25. ValueChanged<DatePickerEntryMode>? onDatePickerModeChange,
  26. Icon? switchToInputEntryModeIcon,
  27. Icon? switchToCalendarEntryModeIcon,
})

Shows a date picker using showDatePicker without requiring a BuildContext.

This function is useful for showing date pickers from anywhere in the app, even outside the widget tree.

Example:

showSyncDatePicker(
  initialDate: DateTime.now(),
  firstDate: DateTime(2022),
  lastDate: DateTime(2025),
);

Implementation

showSyncDatePicker({
  DateTime? initialDate,
  required DateTime firstDate,
  required DateTime lastDate,
  DateTime? currentDate,
  DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
  SelectableDayPredicate? selectableDayPredicate,
  String? helpText,
  String? cancelText,
  String? confirmText,
  Locale? locale,
  bool barrierDismissible = true,
  Color? barrierColor,
  String? barrierLabel,
  bool useRootNavigator = true,
  RouteSettings? routeSettings,
  TextDirection? textDirection,
  TransitionBuilder? builder,
  DatePickerMode initialDatePickerMode = DatePickerMode.day,
  String? errorFormatText,
  String? errorInvalidText,
  String? fieldHintText,
  String? fieldLabelText,
  TextInputType? keyboardType,
  Offset? anchorPoint,
  final ValueChanged<DatePickerEntryMode>? onDatePickerModeChange,
  final Icon? switchToInputEntryModeIcon,
  final Icon? switchToCalendarEntryModeIcon,
}) {
  initialDate = initialDate == null ? null : DateUtils.dateOnly(initialDate);
  firstDate = DateUtils.dateOnly(firstDate);
  lastDate = DateUtils.dateOnly(lastDate);
  assert(
    !lastDate.isBefore(firstDate),
    'lastDate $lastDate must be on or after firstDate $firstDate.',
  );
  assert(
    initialDate == null || !initialDate.isBefore(firstDate),
    'initialDate $initialDate must be on or after firstDate $firstDate.',
  );
  assert(
    initialDate == null || !initialDate.isAfter(lastDate),
    'initialDate $initialDate must be on or before lastDate $lastDate.',
  );
  assert(
    selectableDayPredicate == null ||
        initialDate == null ||
        selectableDayPredicate(initialDate),
    'Provided initialDate $initialDate must satisfy provided selectableDayPredicate.',
  );
  showDatePicker(
      context: Utils.navigatorKey.currentContext!,
      firstDate: firstDate,
      lastDate: lastDate);
}