showSolarDatePicker function

Future<DateTime?> showSolarDatePicker({
  1. required BuildContext context,
  2. required DateTime initialDate,
  3. required DateTime firstDate,
  4. required DateTime lastDate,
  5. bool isPersian = true,
  6. SelectableDayPredicate? selectableDayPredicate,
  7. SolarDatePickerMode initialDatePickerMode = SolarDatePickerMode.day,
  8. Locale? locale,
  9. TextDirection? textDirection,
  10. TransitionBuilder? builder,
  11. bool useRootNavigator = true,
  12. RouteSettings? routeSettings,
  13. Color? headerContentColor,
})

Shows a dialog containing a material design date picker.

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

An optional selectableDayPredicate function can be passed in to customize the days to enable for selection. If provided, only the days that selectableDayPredicate returned true for will be selectable.

An optional initialDatePickerMode argument can be used to display the date picker initially in the year or month+day picker mode. It defaults to month+day, and must not be null.

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 (RTL or LTR) for the date picker. It defaults to the ambient text direction provided by Directionality. If both locale and textDirection are not null, textDirection overrides the direction chosen for the locale.

An optional headerContentColor argument can be used to set the color of date picker header content.

The context, 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.

{@tool snippet} Show a date picker with the dark theme.

Future<DateTime> selectedDate = showDatePicker(
  context: context,
  initialDate: DateTime.now(),
  firstDate: DateTime(2018),
  lastDate: DateTime(2030),
  builder: (BuildContext context, Widget child) {
    return Theme(
      data: ThemeData.dark(),
      child: child,
    );
  },
);

{@end-tool}

The context, initialDate, firstDate, and lastDate parameters must not be null.

See also:

  • showTimePicker, which shows a dialog that contains a material design time picker.
  • SolarDayPicker, which displays the days of a given month and allows choosing a day.
  • SolarMonthPicker, which displays a scrollable list of months to allow picking a month.
  • SolarYearPicker, which displays a scrollable list of years to allow picking a year.

Implementation

Future<DateTime?> showSolarDatePicker({
  required BuildContext context,
  required DateTime initialDate,
  required DateTime firstDate,
  required DateTime lastDate,
  bool isPersian = true,
  SelectableDayPredicate? selectableDayPredicate,
  SolarDatePickerMode initialDatePickerMode = SolarDatePickerMode.day,
  Locale? locale,
  TextDirection? textDirection,
  TransitionBuilder? builder,
  bool useRootNavigator = true,
  RouteSettings? routeSettings,
  Color? headerContentColor,
}) async {
  assert(!initialDate.isBefore(firstDate),
      'initialDate must be on or after firstDate');
  assert(!initialDate.isAfter(lastDate),
      'initialDate must be on or before lastDate');
  assert(
      !firstDate.isAfter(lastDate), 'lastDate must be on or after firstDate');
  assert(selectableDayPredicate == null || selectableDayPredicate(initialDate),
      'Provided initialDate must satisfy provided selectableDayPredicate');
  assert(debugCheckHasMaterialLocalizations(context));

  Widget child = _DatePickerDialog(
    initialDate: initialDate,
    firstDate: firstDate,
    lastDate: lastDate,
    isPersian: isPersian,
    selectableDayPredicate: selectableDayPredicate,
    initialDatePickerMode: initialDatePickerMode,
    headerContentColor: headerContentColor,
  );

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

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

  return showDialog<DateTime>(
    context: context,
    useRootNavigator: useRootNavigator,
    builder: (context) => builder == null ? child : builder(context, child),
    routeSettings: routeSettings,
  );
}