Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1d5087e

Browse files
authored
fix: CupertinoDatePicker.DateAndTime using showDayOfWeek (#155260)
As mentioned in the issue #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.
1 parent 2181480 commit 1d5087e

2 files changed

Lines changed: 84 additions & 38 deletions

File tree

packages/flutter/lib/src/cupertino/date_picker.dart

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -301,43 +301,38 @@ class CupertinoDatePicker extends StatefulWidget {
301301
this.itemExtent = _kItemExtent,
302302
this.selectionOverlayBuilder,
303303
}) : initialDateTime = initialDateTime ?? DateTime.now(),
304-
assert(
305-
itemExtent > 0,
306-
'item extent should be greater than 0',
307-
),
308-
assert(
309-
minuteInterval > 0 && 60 % minuteInterval == 0,
310-
'minute interval is not a positive integer factor of 60',
311-
) {
312-
assert(
313-
mode != CupertinoDatePickerMode.dateAndTime || minimumDate == null || !this.initialDateTime.isBefore(minimumDate!),
314-
'initial date is before minimum date',
315-
);
316-
assert(
317-
mode != CupertinoDatePickerMode.dateAndTime || maximumDate == null || !this.initialDateTime.isAfter(maximumDate!),
318-
'initial date is after maximum date',
319-
);
320-
assert(
321-
(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || (minimumYear >= 1 && this.initialDateTime.year >= minimumYear),
322-
'initial year is not greater than minimum year, or minimum year is not positive',
323-
);
324-
assert(
325-
(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || maximumYear == null || this.initialDateTime.year <= maximumYear!,
326-
'initial year is not smaller than maximum year',
327-
);
328-
assert(
329-
(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || minimumDate == null || !minimumDate!.isAfter(this.initialDateTime),
330-
'initial date ${this.initialDateTime} is not greater than or equal to minimumDate $minimumDate',
331-
);
332-
assert(
333-
(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || maximumDate == null || !maximumDate!.isBefore(this.initialDateTime),
334-
'initial date ${this.initialDateTime} is not less than or equal to maximumDate $maximumDate',
335-
);
336-
assert(
337-
this.initialDateTime.minute % minuteInterval == 0,
338-
'initial minute is not divisible by minute interval',
339-
);
340-
}
304+
assert(itemExtent > 0, 'item extent should be greater than 0'),
305+
assert(minuteInterval > 0 && 60 % minuteInterval == 0, 'minute interval is not a positive integer factor of 60'),
306+
assert(
307+
mode != CupertinoDatePickerMode.dateAndTime || minimumDate == null || !(initialDateTime ?? DateTime.now()).isBefore(minimumDate),
308+
'initial date is before minimum date',
309+
),
310+
assert(
311+
mode != CupertinoDatePickerMode.dateAndTime || maximumDate == null || !(initialDateTime ?? DateTime.now()).isAfter(maximumDate),
312+
'initial date is after maximum date',
313+
),
314+
assert(
315+
(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || (minimumYear >= 1 && (initialDateTime ?? DateTime.now()).year >= minimumYear),
316+
'initial year is not greater than minimum year, or minimum year is not positive',
317+
),
318+
assert(
319+
(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || maximumYear == null || (initialDateTime ?? DateTime.now()).year <= maximumYear,
320+
'initial year is not smaller than maximum year',
321+
),
322+
assert(
323+
(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || minimumDate == null || !minimumDate.isAfter(initialDateTime ?? DateTime.now()),
324+
'initial date ${initialDateTime ?? DateTime.now()} is not greater than or equal to minimumDate $minimumDate',
325+
),
326+
assert(
327+
(mode != CupertinoDatePickerMode.date && mode != CupertinoDatePickerMode.monthYear) || maximumDate == null || !maximumDate.isBefore(initialDateTime ?? DateTime.now()),
328+
'initial date ${initialDateTime ?? DateTime.now()} is not less than or equal to maximumDate $maximumDate',
329+
),
330+
assert(
331+
(mode == CupertinoDatePickerMode.date) || !showDayOfWeek,
332+
'showDayOfWeek is only supported in date mode',
333+
),
334+
assert((initialDateTime ?? DateTime.now()).minute % minuteInterval == 0, 'initial minute is not divisible by minute interval');
335+
341336

342337
/// The mode of the date picker as one of [CupertinoDatePickerMode]. Defaults
343338
/// to [CupertinoDatePickerMode.dateAndTime]. Value cannot change after
@@ -414,7 +409,9 @@ class CupertinoDatePicker extends StatefulWidget {
414409
/// Defaults to null, which disables background painting entirely.
415410
final Color? backgroundColor;
416411

417-
/// Whether to show day of week alongside day. Defaults to false.
412+
/// Whether to show the day of week alongside the day in [CupertinoDatePickerMode.date] mode.
413+
///
414+
/// Defaults to false.
418415
final bool showDayOfWeek;
419416

420417
/// {@macro flutter.cupertino.picker.itemExtent}

packages/flutter/test/cupertino/date_picker_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,55 @@ void main() {
261261
});
262262
});
263263

264+
testWidgets('showDayOfWeek is only supported in date mode', (WidgetTester tester) async {
265+
expect(
266+
() => CupertinoDatePicker(
267+
mode: CupertinoDatePickerMode.date,
268+
onDateTimeChanged: (DateTime _) {},
269+
showDayOfWeek: true,
270+
),
271+
returnsNormally,
272+
);
273+
274+
expect(
275+
() => CupertinoDatePicker(
276+
mode: CupertinoDatePickerMode.time,
277+
onDateTimeChanged: (DateTime _) {},
278+
showDayOfWeek: true,
279+
),
280+
throwsA(isA<AssertionError>().having(
281+
(AssertionError e) => e.message ?? 'Unknown error',
282+
'message',
283+
contains('showDayOfWeek is only supported in date mode'),
284+
)),
285+
);
286+
287+
expect(
288+
() => CupertinoDatePicker(
289+
mode: CupertinoDatePickerMode.monthYear,
290+
onDateTimeChanged: (DateTime _) {},
291+
showDayOfWeek: true,
292+
),
293+
throwsA(isA<AssertionError>().having(
294+
(AssertionError e) => e.message ?? 'Unknown error',
295+
'message',
296+
contains('showDayOfWeek is only supported in date mode'),
297+
)),
298+
);
299+
300+
expect(
301+
() => CupertinoDatePicker(
302+
onDateTimeChanged: (DateTime _) {},
303+
showDayOfWeek: true,
304+
),
305+
throwsA(isA<AssertionError>().having(
306+
(AssertionError e) => e.message ?? 'Unknown error',
307+
'message',
308+
contains('showDayOfWeek is only supported in date mode'),
309+
)),
310+
);
311+
});
312+
264313
testWidgets('picker honors minuteInterval and secondInterval', (WidgetTester tester) async {
265314
late Duration duration;
266315
await tester.pumpWidget(

0 commit comments

Comments
 (0)