showPersianDateRangePicker function

Future<JalaliRange?> showPersianDateRangePicker({
  1. required BuildContext context,
  2. JalaliRange? initialDateRange,
  3. required Jalali firstDate,
  4. required Jalali lastDate,
  5. Jalali? currentDate,
  6. PDatePickerEntryMode initialEntryMode = PDatePickerEntryMode.calendar,
  7. String? helpText,
  8. bool? showEntryModeIcon,
  9. String? cancelText,
  10. String? confirmText,
  11. String? saveText,
  12. String? errorFormatText,
  13. String? errorInvalidText,
  14. String? errorInvalidRangeText,
  15. String? fieldStartHintText,
  16. String? fieldEndHintText,
  17. String? fieldStartLabelText,
  18. String? fieldEndLabelText,
  19. Locale? locale,
  20. bool useRootNavigator = true,
  21. RouteSettings? routeSettings,
  22. TextDirection? textDirection,
  23. TransitionBuilder? builder,
})

Shows a full screen modal dialog containing a Material Design date range picker.

The returned Future resolves to the JalaliRange selected by the user when the user saves their selection. If the user cancels the dialog, null is returned.

If initialDateRange is non-null, then it will be used as the initially selected date range. If it is provided, initialDateRange.start must be before or on initialDateRange.end.

The firstDate is the earliest allowable date. The lastDate is the latest allowable date. Both must be non-null.

If an initial date range is provided, initialDateRange.start and initialDateRange.end must both fall between or on firstDate and lastDate. For all of these Jalali values, only their dates are considered. Their time fields are ignored.

The currentDate represents the current day (i.e. today). This date will be highlighted in the day grid. If null, the date of Jalali.now() will be used.

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

The following optional string parameters allow you to override the default text used for various parts of the dialog:

  • helpText, the label displayed at the top of the dialog.
  • cancelText, the label on the cancel button for the text input mode.
  • confirmText,the label on the ok button for the text input mode.
  • saveText, the label on the save button for the fullscreen calendar mode.
  • errorFormatText, the message used when an input text isn't in a proper date format.
  • errorInvalidText, the message used when an input text isn't a selectable date.
  • errorInvalidRangeText, the message used when the date range is invalid (e.g. start date is after end date).
  • fieldStartHintText, the text used to prompt the user when no text has been entered in the start field.
  • fieldEndHintText, the text used to prompt the user when no text has been entered in the end field.
  • fieldStartLabelText, the label for the start date text input field.
  • fieldEndLabelText, the label for the end date text input field.

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.

See also:

  • showDatePicker, which shows a material design date picker used to select a single date.
  • JalaliRange, which is used to describe a date range.

Implementation

Future<JalaliRange?> showPersianDateRangePicker({
  required BuildContext context,
  JalaliRange? initialDateRange,
  required Jalali firstDate,
  required Jalali lastDate,
  Jalali? currentDate,
  PDatePickerEntryMode initialEntryMode = PDatePickerEntryMode.calendar,
  String? helpText,
  bool? showEntryModeIcon,
  String? cancelText,
  String? confirmText,
  String? saveText,
  String? errorFormatText,
  String? errorInvalidText,
  String? errorInvalidRangeText,
  String? fieldStartHintText,
  String? fieldEndHintText,
  String? fieldStartLabelText,
  String? fieldEndLabelText,
  Locale? locale,
  bool useRootNavigator = true,
  RouteSettings? routeSettings,
  TextDirection? textDirection,
  TransitionBuilder? builder,
}) async {
  assert(context != null);
  assert(
    initialDateRange == null || (initialDateRange.start != null && initialDateRange.end != null),
    'initialDateRange must be null or have non-null start and end dates.',
  );
  assert(
    initialDateRange == null || !initialDateRange.start.isAfter(initialDateRange.end),
    "initialDateRange's start date must not be after it's end date.",
  );
  initialDateRange = initialDateRange == null ? null : utils.datesOnly(initialDateRange);
  assert(firstDate != null);
  firstDate = utils.dateOnly(firstDate);
  assert(lastDate != null);
  lastDate = utils.dateOnly(lastDate);
  assert(
    !lastDate.isBefore(firstDate),
    'lastDate $lastDate must be on or after firstDate $firstDate.',
  );
  assert(
    initialDateRange == null || !initialDateRange.start.isBefore(firstDate),
    "initialDateRange's start date must be on or after firstDate $firstDate.",
  );
  assert(
    initialDateRange == null || !initialDateRange.end.isBefore(firstDate),
    "initialDateRange's end date must be on or after firstDate $firstDate.",
  );
  assert(
    initialDateRange == null || !initialDateRange.start.isAfter(lastDate),
    "initialDateRange's start date must be on or before lastDate $lastDate.",
  );
  assert(
    initialDateRange == null || !initialDateRange.end.isAfter(lastDate),
    "initialDateRange's end date must be on or before lastDate $lastDate.",
  );
  currentDate = utils.dateOnly(currentDate ?? Jalali.now());
  assert(initialEntryMode != null);
  assert(useRootNavigator != null);

  Widget dialog = _DateRangePickerDialog(
    initialDateRange: initialDateRange,
    firstDate: firstDate,
    lastDate: lastDate,
    currentDate: currentDate,
    initialEntryMode: initialEntryMode,
    helpText: helpText,
    showEntryModeIcon: showEntryModeIcon,
    cancelText: cancelText,
    confirmText: confirmText,
    saveText: saveText,
    errorFormatText: errorFormatText,
    errorInvalidText: errorInvalidText,
    errorInvalidRangeText: errorInvalidRangeText,
    fieldStartHintText: fieldStartHintText,
    fieldEndHintText: fieldEndHintText,
    fieldStartLabelText: fieldStartLabelText,
    fieldEndLabelText: fieldEndLabelText,
  );

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

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

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