Skip to content

Commit a199bb4

Browse files
koukibadryuuangzhang
authored andcommitted
feat(CupertinoDatePicker): add a two points time seperator column (flutter#163417)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> This pull request adds a flag attribute to cupertino date picker to show/hide the two points seperator ":" between minutes and hours, this feature add more customization to the cupertino date picker widget. This new flag is by default set to false and it's an optional flag, the default cupertino date picker behavior stays the same. **CupertinoDatePIcker before:** <img src="https://github.com/user-attachments/assets/94f6722d-b466-4272-b90f-6ac3b358ba6d" width="200"/> <img src="https://github.com/user-attachments/assets/125386fd-2b7d-4988-a54e-8893a7f5907b" width="200"/> **CupertinoDatePicker After** <img src="https://github.com/user-attachments/assets/b5a703d9-6c43-410d-819f-f864e9dde159" width="200"/> <img src="https://github.com/user-attachments/assets/26e828dd-81c4-456f-b8c7-7c116d691060" width="200"/> *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* Here's the issue in question for more details about the proposal and the use cases: [Issue#163416](flutter#163416) *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 5335739 commit a199bb4

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ enum _PickerColumnType {
207207
minute,
208208
// AM/PM column in time and dateAndTime mode.
209209
dayPeriod,
210+
// Time separator column in time and dateAndTime mode.
211+
timeSeparator,
210212
}
211213

212214
/// A date picker widget in iOS style.
@@ -303,6 +305,7 @@ class CupertinoDatePicker extends StatefulWidget {
303305
this.dateOrder,
304306
this.backgroundColor,
305307
this.showDayOfWeek = false,
308+
this.showTimeSeparator = false,
306309
this.itemExtent = _kItemExtent,
307310
this.selectionOverlayBuilder,
308311
}) : initialDateTime = initialDateTime ?? DateTime.now(),
@@ -353,6 +356,12 @@ class CupertinoDatePicker extends StatefulWidget {
353356
assert(
354357
(initialDateTime ?? DateTime.now()).minute % minuteInterval == 0,
355358
'initial minute is not divisible by minute interval',
359+
),
360+
assert(
361+
!showTimeSeparator ||
362+
mode == CupertinoDatePickerMode.dateAndTime ||
363+
mode == CupertinoDatePickerMode.time,
364+
'showTimeSeparator is only supported in time or dateAndTime modes',
356365
);
357366

358367
/// The mode of the date picker as one of [CupertinoDatePickerMode]. Defaults
@@ -435,6 +444,16 @@ class CupertinoDatePicker extends StatefulWidget {
435444
/// Defaults to false.
436445
final bool showDayOfWeek;
437446

447+
/// Whether to show the time separator between hour and minute in the time
448+
/// [CupertinoDatePickerMode.time] and datetime [CupertinoDatePickerMode.dateAndTime]
449+
/// picker modes.
450+
///
451+
/// Throws an error if set to true in [CupertinoDatePickerMode.date]
452+
/// and [CupertinoDatePickerMode.monthYear] mode.
453+
///
454+
/// Defaults to false.
455+
final bool showTimeSeparator;
456+
438457
/// {@macro flutter.cupertino.picker.itemExtent}
439458
///
440459
/// Defaults to a value that matches the default iOS date picker wheel.
@@ -549,6 +568,8 @@ class CupertinoDatePicker extends StatefulWidget {
549568
}
550569
case _PickerColumnType.year:
551570
longTexts.add(localizations.datePickerYear(2018));
571+
case _PickerColumnType.timeSeparator:
572+
longTexts.add(':');
552573
}
553574

554575
assert(
@@ -1045,6 +1066,29 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
10451066
);
10461067
}
10471068

1069+
// Builds the time separator column.
1070+
Widget _buildTimeSeparatorWidget(
1071+
double offAxisFraction,
1072+
TransitionBuilder itemPositioningBuilder,
1073+
Widget? selectionOverlay,
1074+
) {
1075+
return ExcludeSemantics(
1076+
child: CupertinoPicker(
1077+
offAxisFraction: offAxisFraction,
1078+
itemExtent: widget.itemExtent,
1079+
useMagnifier: _kUseMagnifier,
1080+
magnification: _kMagnification,
1081+
backgroundColor: widget.backgroundColor,
1082+
squeeze: _kSqueeze,
1083+
onSelectedItemChanged: (int index) {},
1084+
selectionOverlay: selectionOverlay,
1085+
children: List<Widget>.generate(1, (int index) {
1086+
return itemPositioningBuilder(context, Text(':', style: _themeTextStyle(context)));
1087+
}),
1088+
),
1089+
);
1090+
}
1091+
10481092
// One or more pickers have just stopped scrolling.
10491093
void _pickerDidStopScrolling() {
10501094
// Call setState to update the greyed out date/hour/minute/meridiem.
@@ -1118,6 +1162,11 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
11181162
? <_ColumnBuilder>[_buildMinutePicker, _buildHourPicker]
11191163
: <_ColumnBuilder>[_buildHourPicker, _buildMinutePicker];
11201164

1165+
// Adds time separator column if the picker is showing time separator.
1166+
if (widget.showTimeSeparator) {
1167+
columnWidths.insert(1, _getEstimatedColumnWidth(_PickerColumnType.timeSeparator));
1168+
pickerBuilders.insert(1, _buildTimeSeparatorWidget);
1169+
}
11211170
// Adds am/pm column if the picker is not using 24h format.
11221171
if (!widget.use24hFormat) {
11231172
switch (localizations.datePickerDateTimeOrder) {

packages/flutter/test/cupertino/date_picker_test.dart

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,6 +2488,87 @@ void main() {
24882488
expect(widths.indexOf(largestWidth), equals(1));
24892489
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/39998
24902490

2491+
test('showTimeSeparator is only supported in time or dateAndTime mode', () async {
2492+
expect(
2493+
() => CupertinoDatePicker(
2494+
mode: CupertinoDatePickerMode.time,
2495+
onDateTimeChanged: (DateTime _) {},
2496+
showTimeSeparator: true,
2497+
),
2498+
returnsNormally,
2499+
);
2500+
2501+
expect(
2502+
() => CupertinoDatePicker(onDateTimeChanged: (DateTime _) {}, showTimeSeparator: true),
2503+
returnsNormally,
2504+
);
2505+
2506+
expect(
2507+
() => CupertinoDatePicker(
2508+
mode: CupertinoDatePickerMode.date,
2509+
onDateTimeChanged: (DateTime _) {},
2510+
showTimeSeparator: true,
2511+
),
2512+
throwsA(
2513+
isA<AssertionError>().having(
2514+
(AssertionError e) => e.message ?? 'Unknown error',
2515+
'message',
2516+
contains('showTimeSeparator is only supported in time or dateAndTime modes'),
2517+
),
2518+
),
2519+
);
2520+
2521+
expect(
2522+
() => CupertinoDatePicker(
2523+
mode: CupertinoDatePickerMode.monthYear,
2524+
onDateTimeChanged: (DateTime _) {},
2525+
showTimeSeparator: true,
2526+
),
2527+
throwsA(
2528+
isA<AssertionError>().having(
2529+
(AssertionError e) => e.message ?? 'Unknown error',
2530+
'message',
2531+
contains('showTimeSeparator is only supported in time or dateAndTime modes'),
2532+
),
2533+
),
2534+
);
2535+
});
2536+
2537+
testWidgets('Time separator widget should be rendered when flag is set to true', (
2538+
WidgetTester tester,
2539+
) async {
2540+
await tester.pumpWidget(
2541+
CupertinoApp(
2542+
home: Center(
2543+
child: CupertinoDatePicker(
2544+
mode: CupertinoDatePickerMode.time,
2545+
onDateTimeChanged: (DateTime dateTime) {},
2546+
showTimeSeparator: true,
2547+
),
2548+
),
2549+
),
2550+
);
2551+
2552+
expect(find.text(':'), findsOneWidget);
2553+
});
2554+
2555+
testWidgets('Time separator widget should not be rendered when flag is set to false', (
2556+
WidgetTester tester,
2557+
) async {
2558+
await tester.pumpWidget(
2559+
CupertinoApp(
2560+
home: Center(
2561+
child: CupertinoDatePicker(
2562+
mode: CupertinoDatePickerMode.time,
2563+
onDateTimeChanged: (DateTime _) {},
2564+
),
2565+
),
2566+
),
2567+
);
2568+
2569+
expect(find.text(':'), findsNothing);
2570+
});
2571+
24912572
// Regression test for https://github.com/flutter/flutter/issues/161773
24922573
testWidgets('CupertinoDatePicker date value baseline alignment', (WidgetTester tester) async {
24932574
await tester.pumpWidget(

0 commit comments

Comments
 (0)