pickDate static method
Future<DateTime?>
pickDate({
- required BuildContext context,
- DateTime? initialDate,
- DateTime? firstDate,
- DateTime? lastDate,
- PickerStyle style = PickerStyle.platform,
- String doneLabel = 'Done',
- String cancelLabel = 'Cancel',
- Locale? locale,
- Color? backgroundColor,
- Color? accentColor,
Implementation
static Future<DateTime?> pickDate({
required BuildContext context,
DateTime? initialDate,
DateTime? firstDate,
DateTime? lastDate,
PickerStyle style = PickerStyle.platform,
String doneLabel = 'Done',
String cancelLabel = 'Cancel',
Locale? locale,
Color? backgroundColor,
Color? accentColor,
}) async {
// A date picker only cares about the calendar day, so drop the time from
// every bound (`dateOnly()` returns a new value — it does not mutate). This
// prevents a sub-second gap between two `DateTime.now()` values from making
// `initialDate` fall just before `firstDate` and tripping the picker's
// min/max assertions. The initial value is also clamped into range.
final DateTime first = (firstDate ?? DateTime(1900)).dateOnly();
final DateTime last = (lastDate ?? DateTime(2100)).dateOnly();
DateTime initial = (initialDate ?? DateTime.now()).dateOnly();
if (initial.isBefore(first)) initial = first;
if (initial.isAfter(last)) initial = last;
final PickerStyle pickerStyle = _resolveStyle(context, style);
if (pickerStyle == PickerStyle.ios) {
return _cupertinoPicker(
context: context,
initialDate: initial,
mode: CupertinoDatePickerMode.date,
doneLabel: doneLabel,
cancelLabel: cancelLabel,
minimumDate: first,
maximumDate: last,
backgroundColor: backgroundColor,
accentColor: accentColor,
);
}
return showDatePicker(
context: context,
initialDate: initial,
firstDate: first,
lastDate: last,
locale: locale,
builder: (BuildContext ctx, Widget? child) =>
_applyAccent(ctx, child, accentColor),
);
}