fix: CupertinoDatePicker.DateAndTime using showDayOfWeek (#155260)
As mentioned in the issue https://github.com/flutter/flutter/issues/153576, even if we declare the 'showDayOfWeek' value as false, when using the mode CupertinoDatePickerMode.dateAndTime, the days of week still shows in the screen. This PR is about making an assert that prevent developers from using `DateTimePicker.DateAndTime` with `showDayOfWeek` as false (because it only works on date mode) as suggested by @Piinks.
This commit is contained in:
parent
21814805c1
commit
1d5087eb30
@ -301,43 +301,38 @@ class CupertinoDatePicker extends StatefulWidget {
|
|||||||
this.itemExtent = _kItemExtent,
|
this.itemExtent = _kItemExtent,
|
||||||
this.selectionOverlayBuilder,
|
this.selectionOverlayBuilder,
|
||||||
}) : initialDateTime = initialDateTime ?? DateTime.now(),
|
}) : initialDateTime = initialDateTime ?? DateTime.now(),
|
||||||
assert(
|
assert(itemExtent > 0, 'item extent should be greater than 0'),
|
||||||
itemExtent > 0,
|
assert(minuteInterval > 0 && 60 % minuteInterval == 0, 'minute interval is not a positive integer factor of 60'),
|
||||||
'item extent should be greater than 0',
|
assert(
|
||||||
),
|
mode != CupertinoDatePickerMode.dateAndTime || minimumDate == null || !(initialDateTime ?? DateTime.now()).isBefore(minimumDate),
|
||||||
assert(
|
'initial date is before minimum date',
|
||||||
minuteInterval > 0 && 60 % minuteInterval == 0,
|
),
|
||||||
'minute interval is not a positive integer factor of 60',
|
assert(
|
||||||
) {
|
mode != CupertinoDatePickerMode.dateAndTime || maximumDate == null || !(initialDateTime ?? DateTime.now()).isAfter(maximumDate),
|
||||||
assert(
|
'initial date is after maximum date',
|
||||||
mode != CupertinoDatePickerMode.dateAndTime || minimumDate == null || !this.initialDateTime.isBefore(minimumDate!),
|
),
|
||||||
'initial date is before minimum date',
|
assert(
|
||||||
);
|
(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || (minimumYear >= 1 && (initialDateTime ?? DateTime.now()).year >= minimumYear),
|
||||||
assert(
|
'initial year is not greater than minimum year, or minimum year is not positive',
|
||||||
mode != CupertinoDatePickerMode.dateAndTime || maximumDate == null || !this.initialDateTime.isAfter(maximumDate!),
|
),
|
||||||
'initial date is after maximum date',
|
assert(
|
||||||
);
|
(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || maximumYear == null || (initialDateTime ?? DateTime.now()).year <= maximumYear,
|
||||||
assert(
|
'initial year is not smaller than maximum year',
|
||||||
(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) || minimumDate == null || !minimumDate.isAfter(initialDateTime ?? DateTime.now()),
|
||||||
assert(
|
'initial date ${initialDateTime ?? DateTime.now()} is not greater than or equal to minimumDate $minimumDate',
|
||||||
(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) || maximumDate == null || !maximumDate.isBefore(initialDateTime ?? DateTime.now()),
|
||||||
assert(
|
'initial date ${initialDateTime ?? DateTime.now()} is not less than or equal to maximumDate $maximumDate',
|
||||||
(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) || !showDayOfWeek,
|
||||||
assert(
|
'showDayOfWeek is only supported in date mode',
|
||||||
(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((initialDateTime ?? DateTime.now()).minute % minuteInterval == 0, 'initial minute is not divisible by minute interval');
|
||||||
);
|
|
||||||
assert(
|
|
||||||
this.initialDateTime.minute % minuteInterval == 0,
|
|
||||||
'initial minute is not divisible by minute interval',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The mode of the date picker as one of [CupertinoDatePickerMode]. Defaults
|
/// The mode of the date picker as one of [CupertinoDatePickerMode]. Defaults
|
||||||
/// to [CupertinoDatePickerMode.dateAndTime]. Value cannot change after
|
/// to [CupertinoDatePickerMode.dateAndTime]. Value cannot change after
|
||||||
@ -414,7 +409,9 @@ class CupertinoDatePicker extends StatefulWidget {
|
|||||||
/// Defaults to null, which disables background painting entirely.
|
/// Defaults to null, which disables background painting entirely.
|
||||||
final Color? backgroundColor;
|
final Color? backgroundColor;
|
||||||
|
|
||||||
/// Whether to show day of week alongside day. Defaults to false.
|
/// Whether to show the day of week alongside the day in [CupertinoDatePickerMode.date] mode.
|
||||||
|
///
|
||||||
|
/// Defaults to false.
|
||||||
final bool showDayOfWeek;
|
final bool showDayOfWeek;
|
||||||
|
|
||||||
/// {@macro flutter.cupertino.picker.itemExtent}
|
/// {@macro flutter.cupertino.picker.itemExtent}
|
||||||
|
@ -261,6 +261,55 @@ void main() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('showDayOfWeek is only supported in date mode', (WidgetTester tester) async {
|
||||||
|
expect(
|
||||||
|
() => CupertinoDatePicker(
|
||||||
|
mode: CupertinoDatePickerMode.date,
|
||||||
|
onDateTimeChanged: (DateTime _) {},
|
||||||
|
showDayOfWeek: true,
|
||||||
|
),
|
||||||
|
returnsNormally,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
() => CupertinoDatePicker(
|
||||||
|
mode: CupertinoDatePickerMode.time,
|
||||||
|
onDateTimeChanged: (DateTime _) {},
|
||||||
|
showDayOfWeek: true,
|
||||||
|
),
|
||||||
|
throwsA(isA<AssertionError>().having(
|
||||||
|
(AssertionError e) => e.message ?? 'Unknown error',
|
||||||
|
'message',
|
||||||
|
contains('showDayOfWeek is only supported in date mode'),
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
() => CupertinoDatePicker(
|
||||||
|
mode: CupertinoDatePickerMode.monthYear,
|
||||||
|
onDateTimeChanged: (DateTime _) {},
|
||||||
|
showDayOfWeek: true,
|
||||||
|
),
|
||||||
|
throwsA(isA<AssertionError>().having(
|
||||||
|
(AssertionError e) => e.message ?? 'Unknown error',
|
||||||
|
'message',
|
||||||
|
contains('showDayOfWeek is only supported in date mode'),
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
() => CupertinoDatePicker(
|
||||||
|
onDateTimeChanged: (DateTime _) {},
|
||||||
|
showDayOfWeek: true,
|
||||||
|
),
|
||||||
|
throwsA(isA<AssertionError>().having(
|
||||||
|
(AssertionError e) => e.message ?? 'Unknown error',
|
||||||
|
'message',
|
||||||
|
contains('showDayOfWeek is only supported in date mode'),
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
testWidgets('picker honors minuteInterval and secondInterval', (WidgetTester tester) async {
|
testWidgets('picker honors minuteInterval and secondInterval', (WidgetTester tester) async {
|
||||||
late Duration duration;
|
late Duration duration;
|
||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user