showDatePicker function

Future<DateTime> showDatePicker ({@required BuildContext context, @required DateTime initialDate, @required DateTime firstDate, @required DateTime lastDate, SelectableDayPredicate selectableDayPredicate, DatePickerMode initialDatePickerMode: DatePickerMode.day, Locale locale, TextDirection textDirection })

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.

The context argument is passed to showDialog, the documentation for which discusses how it is used.

See also:

Implementation

Future<DateTime> showDatePicker({
  @required BuildContext context,
  @required DateTime initialDate,
  @required DateTime firstDate,
  @required DateTime lastDate,
  SelectableDayPredicate selectableDayPredicate,
  DatePickerMode initialDatePickerMode: DatePickerMode.day,
  Locale locale,
  TextDirection textDirection,
}) 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(initialDatePickerMode != null, 'initialDatePickerMode must not be null');

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

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

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

  return await showDialog<DateTime>(
    context: context,
    builder: (BuildContext context) => child,
  );
}