CupertinoDatePicker constructor

CupertinoDatePicker({
  1. Key? key,
  2. CupertinoDatePickerMode mode = CupertinoDatePickerMode.dateAndTime,
  3. required ValueChanged<DateTime> onDateTimeChanged,
  4. DateTime? initialDateTime,
  5. DateTime? minimumDate,
  6. DateTime? maximumDate,
  7. int minimumYear = 1,
  8. int? maximumYear,
  9. int minuteInterval = 1,
  10. bool use24hFormat = false,
  11. DatePickerDateOrder? dateOrder,
  12. Color? backgroundColor,
  13. bool showDayOfWeek = false,
  14. double itemExtent = _kItemExtent,
  15. SelectionOverlayBuilder? selectionOverlayBuilder,
})

Constructs an iOS style date picker.

mode is one of the mode listed in CupertinoDatePickerMode and defaults to CupertinoDatePickerMode.dateAndTime.

onDateTimeChanged is the callback called when the selected date or time changes. When in CupertinoDatePickerMode.time mode, the year, month and day will be the same as initialDateTime. When in CupertinoDatePickerMode.date mode, this callback will always report the start time of the currently selected day. When in CupertinoDatePickerMode.monthYear mode, the day and time will be the start time of the first day of the month.

initialDateTime is the initial date time of the picker. Defaults to the present date and time. The present must conform to the intervals set in minimumDate, maximumDate, minimumYear, and maximumYear.

minimumDate is the minimum selectable DateTime of the picker. When set to null, the picker does not limit the minimum DateTime the user can pick. In CupertinoDatePickerMode.time mode, minimumDate should typically be on the same date as initialDateTime, as the picker will not limit the minimum time the user can pick if it's set to a date earlier than that.

maximumDate is the maximum selectable DateTime of the picker. When set to null, the picker does not limit the maximum DateTime the user can pick. In CupertinoDatePickerMode.time mode, maximumDate should typically be on the same date as initialDateTime, as the picker will not limit the maximum time the user can pick if it's set to a date later than that.

minimumYear is the minimum year that the picker can be scrolled to in CupertinoDatePickerMode.date mode. Defaults to 1.

maximumYear is the maximum year that the picker can be scrolled to in CupertinoDatePickerMode.date mode. Null if there's no limit.

minuteInterval is the granularity of the minute spinner. Must be a positive integer factor of 60.

use24hFormat decides whether 24 hour format is used. Defaults to false.

dateOrder determines the order of the columns inside CupertinoDatePicker in CupertinoDatePickerMode.date and CupertinoDatePickerMode.monthYear mode. When using monthYear mode, both DatePickerDateOrder.dmy and DatePickerDateOrder.mdy will result in the month|year order. Defaults to the locale's default date format/order.

Implementation

CupertinoDatePicker({
  super.key,
  this.mode = CupertinoDatePickerMode.dateAndTime,
  required this.onDateTimeChanged,
  DateTime? initialDateTime,
  this.minimumDate,
  this.maximumDate,
  this.minimumYear = 1,
  this.maximumYear,
  this.minuteInterval = 1,
  this.use24hFormat = false,
  this.dateOrder,
  this.backgroundColor,
  this.showDayOfWeek = false,
  this.itemExtent = _kItemExtent,
  this.selectionOverlayBuilder,
}) : initialDateTime = initialDateTime ?? DateTime.now(),
     assert(
       itemExtent > 0,
       'item extent should be greater than 0',
     ),
     assert(
       minuteInterval > 0 && 60 % minuteInterval == 0,
       'minute interval is not a positive integer factor of 60',
     ) {
  assert(
    mode != CupertinoDatePickerMode.dateAndTime || minimumDate == null || !this.initialDateTime.isBefore(minimumDate!),
    'initial date is before minimum date',
  );
  assert(
    mode != CupertinoDatePickerMode.dateAndTime || maximumDate == null || !this.initialDateTime.isAfter(maximumDate!),
    'initial date is after maximum date',
  );
  assert(
    (mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || (minimumYear >= 1 && this.initialDateTime.year >= minimumYear),
    'initial year is not greater than minimum year, or minimum year is not positive',
  );
  assert(
    (mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || maximumYear == null || this.initialDateTime.year <= maximumYear!,
    'initial year is not smaller than maximum year',
  );
  assert(
    (mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || minimumDate == null || !minimumDate!.isAfter(this.initialDateTime),
    'initial date ${this.initialDateTime} is not greater than or equal to minimumDate $minimumDate',
  );
  assert(
    (mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || maximumDate == null || !maximumDate!.isBefore(this.initialDateTime),
    'initial date ${this.initialDateTime} is not less than or equal to maximumDate $maximumDate',
  );
  assert(
    this.initialDateTime.minute % minuteInterval == 0,
    'initial minute is not divisible by minute interval',
  );
}