showPersianDatePicker function

Future<Jalali?> showPersianDatePicker({
  1. required BuildContext context,
  2. required Jalali initialDate,
  3. required Jalali firstDate,
  4. required Jalali lastDate,
  5. PDatePickerEntryMode initialEntryMode = PDatePickerEntryMode.calendar,
  6. PSelectableDayPredicate? selectableDayPredicate,
  7. String? helpText,
  8. String? cancelText,
  9. String? confirmText,
  10. Locale? locale,
  11. bool useRootNavigator = true,
  12. RouteSettings? routeSettings,
  13. TextDirection? textDirection,
  14. TransitionBuilder? builder,
  15. PDatePickerMode initialDatePickerMode = PDatePickerMode.day,
  16. String? errorFormatText,
  17. String? errorInvalidText,
  18. String fieldHintText = '##/##/####',
  19. String fieldLabelText = 'ورود تاریخ',
})

Shows a dialog containing a Material Design date picker.

The returned Future resolves to the date selected by the user when the user confirms the dialog. If the user cancels the dialog, null is returned.

When the date picker is first displayed, it will show the month of initialDate, with initialDate selected.

The firstDate is the earliest allowable date. The lastDate is the latest allowable date. initialDate must either fall between these dates, or be equal to one of them. For each of these Jalali parameters, only their dates are considered. Their time fields are ignored. They must all be non-null.

An optional initialEntryMode argument can be used to display the date picker in the PDatePickerEntryMode.calendar (a calendar month grid) or PDatePickerEntryMode.input (a text input field) mode. It defaults to PDatePickerEntryMode.calendar and must be non-null.

An optional selectableDayPredicate function can be passed in to only allow certain days for selection. If provided, only the days that selectableDayPredicate returns true for will be selectable. For example, this can be used to only allow weekdays for selection. If provided, it must return true for initialDate.

Optional strings for the cancelText, confirmText, errorFormatText, errorInvalidText, fieldHintText, fieldLabelText, and helpText allow you to override the default text used for various parts of the dialog:

  • cancelText, label on the cancel button.
  • confirmText, label on the ok button.
  • errorFormatText, message used when the input text isn't in a proper date format.
  • errorInvalidText, message used when the input text isn't a selectable date.
  • fieldHintText, text used to prompt the user when no text has been entered in the field.
  • fieldLabelText, label for the date text input field.
  • helpText, label on the top of the dialog.

An optional locale argument can be used to set the locale for the date picker. It defaults to the ambient locale provided by Localizations.

An optional textDirection argument can be used to set the text direction (TextDirection.ltr or TextDirection.rtl) for the date picker. It defaults to the ambient text direction provided by Directionality. If both locale and textDirection are non-null, textDirection overrides the direction chosen for the locale.

The context, useRootNavigator and routeSettings arguments are passed to showDialog, the documentation for which discusses how it is used. context and useRootNavigator must be non-null.

The builder parameter can be used to wrap the dialog widget to add inherited widgets like Theme.

An optional initialDatePickerMode argument can be used to have the calendar date picker initially appear in the DatePickerMode.year or DatePickerMode.day mode. It defaults to DatePickerMode.day, and must be non-null.

Implementation

Future<Jalali?> showPersianDatePicker({
  required BuildContext context,
  required Jalali initialDate,
  required Jalali firstDate,
  required Jalali lastDate,
  PDatePickerEntryMode initialEntryMode = PDatePickerEntryMode.calendar,
  PSelectableDayPredicate? selectableDayPredicate,
  String? helpText,
  String? cancelText,
  String? confirmText,
  Locale? locale,
  bool useRootNavigator = true,
  RouteSettings? routeSettings,
  TextDirection? textDirection,
  TransitionBuilder? builder,
  PDatePickerMode initialDatePickerMode = PDatePickerMode.day,
  String? errorFormatText,
  String? errorInvalidText,
  String fieldHintText = '##/##/####',
  String fieldLabelText = 'ورود تاریخ',
}) async {
  initialDate = utils.dateOnly(initialDate);
  firstDate = utils.dateOnly(firstDate);
  lastDate = utils.dateOnly(lastDate);
  assert(!lastDate.isBefore(firstDate),
      'lastDate $lastDate must be on or after firstDate $firstDate.');
  assert(!initialDate.isBefore(firstDate),
      'initialDate $initialDate must be on or after firstDate $firstDate.');
  assert(!initialDate.isAfter(lastDate),
      'initialDate $initialDate must be on or before lastDate $lastDate.');
  assert(selectableDayPredicate == null || selectableDayPredicate(initialDate),
      'Provided initialDate $initialDate must satisfy provided selectableDayPredicate.');
  assert(debugCheckHasMaterialLocalizations(context));

  Widget dialog = _DatePickerDialog(
    initialDate: initialDate,
    firstDate: firstDate,
    lastDate: lastDate,
    initialEntryMode: initialEntryMode,
    selectableDayPredicate: selectableDayPredicate,
    helpText: helpText,
    cancelText: cancelText,
    confirmText: confirmText,
    initialCalendarMode: initialDatePickerMode,
    errorFormatText: errorFormatText,
    errorInvalidText: errorInvalidText,
    fieldHintText: fieldHintText,
    fieldLabelText: fieldLabelText,
  );

  if (textDirection != null) {
    dialog = Directionality(
      textDirection: textDirection,
      child: dialog,
    );
  }

  if (locale != null) {
    dialog = Localizations.override(
      context: context,
      locale: locale,
      child: dialog,
    );
  }

  return showDialog<Jalali>(
    context: context,
    useRootNavigator: useRootNavigator,
    routeSettings: routeSettings,
    builder: (BuildContext context) {
      return builder == null ? dialog : builder(context, dialog);
    },
  );
}