pickDate static method

Future<DateTime?> pickDate({
  1. required BuildContext context,
  2. DateTime? initialDate,
  3. DateTime? firstDate,
  4. DateTime? lastDate,
  5. PickerStyle style = PickerStyle.platform,
  6. String doneLabel = 'Done',
  7. String cancelLabel = 'Cancel',
  8. Locale? locale,
  9. Color? backgroundColor,
  10. 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),
  );
}