CupertinoDatePicker constructor Null safety

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}
)

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 and must not be null. 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.

initialDateTime is the initial date time of the picker. Defaults to the present date and time and must not be null. 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 and must not be null.

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 date mode. 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,
}) : initialDateTime = initialDateTime ?? DateTime.now(),
     assert(mode != null),
     assert(onDateTimeChanged != null),
     assert(minimumYear != null),
     assert(
       minuteInterval > 0 && 60 % minuteInterval == 0,
       'minute interval is not a positive integer factor of 60',
     ) {
  assert(this.initialDateTime != null);
  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 || (minimumYear >= 1 && this.initialDateTime.year >= minimumYear),
    'initial year is not greater than minimum year, or minimum year is not positive',
  );
  assert(
    mode != CupertinoDatePickerMode.date || maximumYear == null || this.initialDateTime.year <= maximumYear!,
    'initial year is not smaller than maximum year',
  );
  assert(
    mode != CupertinoDatePickerMode.date || minimumDate == null || !minimumDate!.isAfter(this.initialDateTime),
    'initial date ${this.initialDateTime} is not greater than or equal to minimumDate $minimumDate',
  );
  assert(
    mode != CupertinoDatePickerMode.date || 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',
  );
}