showPersianDatePicker function
- required BuildContext context,
- Jalali? initialDate,
- required Jalali firstDate,
- required Jalali lastDate,
- Jalali? currentDate,
- PersianDatePickerEntryMode initialEntryMode = PersianDatePickerEntryMode.calendar,
- PersianSelectableDayPredicate? selectableDayPredicate,
- String? helpText,
- String? cancelText,
- String? confirmText,
- Locale? locale,
- bool barrierDismissible = true,
- Color? barrierColor,
- String? barrierLabel,
- RouteSettings? routeSettings,
- TextDirection? textDirection,
- TransitionBuilder? builder,
- PersianDatePickerMode initialDatePickerMode = PersianDatePickerMode.day,
- String? errorFormatText,
- String? errorInvalidText,
- String? fieldHintText,
- String? fieldLabelText,
- TextInputType? keyboardType,
- Offset? anchorPoint,
- ValueChanged<
PersianDatePickerEntryMode> ? onDatePickerModeChange, - Icon? switchToInputEntryModeIcon,
- Icon? switchToCalendarEntryModeIcon,
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, if initialDate
is not null, it
will show the month of initialDate
, with initialDate
selected. Otherwise
it will show the currentDate
's month.
The firstDate
is the earliest allowable date. The lastDate
is the latest
allowable date. If initialDate
is not null, it 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.
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 PersianDatePickerEntryMode.calendar (a calendar month grid)
or PersianDatePickerEntryMode.input (a text input field) mode.
It defaults to PersianDatePickerEntryMode.calendar.
An optional switchToInputEntryModeIcon
argument can be used to
display a custom Icon in the corner of the dialog
when DatePickerEntryMode is DatePickerEntryMode.calendar. Clicking on
icon changes the DatePickerEntryMode to DatePickerEntryMode.input.
If null, Icon(useMaterial3 ? Icons.edit_outlined : Icons.edit)
is used.
An optional switchToCalendarEntryModeIcon
argument can be used to
display a custom Icon in the corner of the dialog
when DatePickerEntryMode is DatePickerEntryMode.input. Clicking on
icon changes the DatePickerEntryMode to DatePickerEntryMode.calendar.
If null, Icon(Icons.calendar_today)
is used.
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
.
The following optional string parameters allow you to override the default text used for various parts of the dialog:
helpText
, label displayed at the top 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.
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
, barrierDismissible
, barrierColor
, barrierLabel
,
useRootNavigator
and routeSettings
arguments are passed to showDialog,
the documentation for which discusses how it is used.
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 PersianDatePickerMode.year or
PersianDatePickerMode.day mode. It defaults to PersianDatePickerMode.day.
A DisplayFeature can split the screen into sub-screens. The closest one to
anchorPoint
is used to render the content.
If no anchorPoint
is provided, then Directionality is used:
- for TextDirection.ltr,
anchorPoint
isOffset.zero
, which will cause the content to appear in the top-left sub-screen. - for TextDirection.rtl,
anchorPoint
isOffset(double.maxFinite, 0)
, which will cause the content to appear in the top-right sub-screen.
If no anchorPoint
is provided, and there is no Directionality ancestor
widget in the tree, then the widget asserts during build in debug mode.
State Restoration
Using this method will not enable state restoration for the date picker. In order to enable state restoration for a date picker, use Navigator.restorablePush or Navigator.restorablePushNamed with DatePickerDialog.
For more information about state restoration, see RestorationManager.
To test state restoration on Android:
- Turn on "Don't keep activities", which destroys the Android activity as soon as the user leaves it. This option should become available when Developer Options are turned on for the device.
- Run the code sample on an Android device.
- Create some in-memory state in the app on the phone, e.g. by navigating to a different screen.
- Background the Flutter app, then return to it. It will restart and restore its state.
To test state restoration on iOS:
- Open
ios/Runner.xcworkspace/
in Xcode. - (iOS 14+ only): Switch to build in profile or release mode, as launching an app from the home screen is not supported in debug mode.
- Press the Play button in Xcode to build and run the app.
- Create some in-memory state in the app on the phone, e.g. by navigating to a different screen.
- Background the app on the phone, e.g. by going back to the home screen.
- Press the Stop button in Xcode to terminate the app while running in the background.
- Open the app again on the phone (not via Xcode). It will restart and restore its state.
{@tool dartpad} This sample demonstrates how to create a restorable Material date picker. This is accomplished by enabling state restoration by specifying MaterialApp.restorationScopeId and using Navigator.restorablePush to push DatePickerDialog when the button is tapped.
** See code in examples/api/lib/material/date_picker/show_date_picker.0.dart ** {@end-tool}
See also:
- showPersianDateRangePicker, which shows a Material Design date range picker used to select a range of dates.
- PersianCalendarDatePicker, which provides the calendar grid used by the date picker dialog.
- PersianInputDatePickerFormField, which provides a text input field for entering dates.
- DisplayFeatureSubScreen, which documents the specifics of how DisplayFeatures can split the screen into sub-screens.
- showTimePicker, which shows a dialog that contains a Material Design time picker.
Implementation
Future<Jalali?> showPersianDatePicker({
required BuildContext context,
Jalali? initialDate,
required Jalali firstDate,
required Jalali lastDate,
Jalali? currentDate,
PersianDatePickerEntryMode initialEntryMode =
PersianDatePickerEntryMode.calendar,
PersianSelectableDayPredicate? 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,
PersianDatePickerMode initialDatePickerMode = PersianDatePickerMode.day,
String? errorFormatText,
String? errorInvalidText,
String? fieldHintText,
String? fieldLabelText,
TextInputType? keyboardType,
Offset? anchorPoint,
final ValueChanged<PersianDatePickerEntryMode>? onDatePickerModeChange,
final Icon? switchToInputEntryModeIcon,
final Icon? switchToCalendarEntryModeIcon,
}) async {
initialDate =
initialDate == null ? null : PersianDateUtils.dateOnly(initialDate);
firstDate = PersianDateUtils.dateOnly(firstDate);
lastDate = PersianDateUtils.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.',
);
assert(debugCheckHasMaterialLocalizations(context));
Widget dialog = DatePickerDialog(
initialDate: initialDate,
firstDate: firstDate,
lastDate: lastDate,
currentDate: currentDate,
initialEntryMode: initialEntryMode,
selectableDayPredicate: selectableDayPredicate,
helpText: helpText,
cancelText: cancelText,
confirmText: confirmText,
initialCalendarMode: initialDatePickerMode,
errorFormatText: errorFormatText,
errorInvalidText: errorInvalidText,
fieldHintText: fieldHintText,
fieldLabelText: fieldLabelText,
keyboardType: keyboardType,
onDatePickerModeChange: onDatePickerModeChange,
switchToInputEntryModeIcon: switchToInputEntryModeIcon,
switchToCalendarEntryModeIcon: switchToCalendarEntryModeIcon,
);
if (textDirection != null) {
dialog = Directionality(
textDirection: textDirection,
child: dialog,
);
}
if (locale != null) {
dialog = Localizations.override(
context: context,
locale: locale,
child: dialog,
);
} else {
final DatePickerThemeData datePickerTheme =
DatePickerTheme.of(context);
if (datePickerTheme.locale != null) {
dialog = Localizations.override(
context: context,
locale: datePickerTheme.locale,
child: dialog,
);
}
}
return showDialog<Jalali>(
context: context,
barrierDismissible: barrierDismissible,
barrierColor: barrierColor,
barrierLabel: barrierLabel,
useRootNavigator: useRootNavigator,
routeSettings: routeSettings,
builder: (BuildContext context) {
return builder == null ? dialog : builder(context, dialog);
},
anchorPoint: anchorPoint,
);
}