feat(CupertinoDatePicker): add a two points time seperator column (#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](https://github.com/flutter/flutter/issues/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
This commit is contained in:
Kouki Badr
2025-03-24 19:37:54 +01:00
committed by GitHub
parent 5db3c8844e
commit 2c58a631fe
2 changed files with 130 additions and 0 deletions

View File

@@ -207,6 +207,8 @@ enum _PickerColumnType {
minute,
// AM/PM column in time and dateAndTime mode.
dayPeriod,
// Time separator column in time and dateAndTime mode.
timeSeparator,
}
/// A date picker widget in iOS style.
@@ -303,6 +305,7 @@ class CupertinoDatePicker extends StatefulWidget {
this.dateOrder,
this.backgroundColor,
this.showDayOfWeek = false,
this.showTimeSeparator = false,
this.itemExtent = _kItemExtent,
this.selectionOverlayBuilder,
}) : initialDateTime = initialDateTime ?? DateTime.now(),
@@ -353,6 +356,12 @@ class CupertinoDatePicker extends StatefulWidget {
assert(
(initialDateTime ?? DateTime.now()).minute % minuteInterval == 0,
'initial minute is not divisible by minute interval',
),
assert(
!showTimeSeparator ||
mode == CupertinoDatePickerMode.dateAndTime ||
mode == CupertinoDatePickerMode.time,
'showTimeSeparator is only supported in time or dateAndTime modes',
);
/// The mode of the date picker as one of [CupertinoDatePickerMode]. Defaults
@@ -435,6 +444,16 @@ class CupertinoDatePicker extends StatefulWidget {
/// Defaults to false.
final bool showDayOfWeek;
/// Whether to show the time separator between hour and minute in the time
/// [CupertinoDatePickerMode.time] and datetime [CupertinoDatePickerMode.dateAndTime]
/// picker modes.
///
/// Throws an error if set to true in [CupertinoDatePickerMode.date]
/// and [CupertinoDatePickerMode.monthYear] mode.
///
/// Defaults to false.
final bool showTimeSeparator;
/// {@macro flutter.cupertino.picker.itemExtent}
///
/// Defaults to a value that matches the default iOS date picker wheel.
@@ -549,6 +568,8 @@ class CupertinoDatePicker extends StatefulWidget {
}
case _PickerColumnType.year:
longTexts.add(localizations.datePickerYear(2018));
case _PickerColumnType.timeSeparator:
longTexts.add(':');
}
assert(
@@ -1045,6 +1066,29 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
);
}
// Builds the time separator column.
Widget _buildTimeSeparatorWidget(
double offAxisFraction,
TransitionBuilder itemPositioningBuilder,
Widget? selectionOverlay,
) {
return ExcludeSemantics(
child: CupertinoPicker(
offAxisFraction: offAxisFraction,
itemExtent: widget.itemExtent,
useMagnifier: _kUseMagnifier,
magnification: _kMagnification,
backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze,
onSelectedItemChanged: (int index) {},
selectionOverlay: selectionOverlay,
children: List<Widget>.generate(1, (int index) {
return itemPositioningBuilder(context, Text(':', style: _themeTextStyle(context)));
}),
),
);
}
// One or more pickers have just stopped scrolling.
void _pickerDidStopScrolling() {
// Call setState to update the greyed out date/hour/minute/meridiem.
@@ -1118,6 +1162,11 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
? <_ColumnBuilder>[_buildMinutePicker, _buildHourPicker]
: <_ColumnBuilder>[_buildHourPicker, _buildMinutePicker];
// Adds time separator column if the picker is showing time separator.
if (widget.showTimeSeparator) {
columnWidths.insert(1, _getEstimatedColumnWidth(_PickerColumnType.timeSeparator));
pickerBuilders.insert(1, _buildTimeSeparatorWidget);
}
// Adds am/pm column if the picker is not using 24h format.
if (!widget.use24hFormat) {
switch (localizations.datePickerDateTimeOrder) {

View File

@@ -2488,6 +2488,87 @@ void main() {
expect(widths.indexOf(largestWidth), equals(1));
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/39998
test('showTimeSeparator is only supported in time or dateAndTime mode', () async {
expect(
() => CupertinoDatePicker(
mode: CupertinoDatePickerMode.time,
onDateTimeChanged: (DateTime _) {},
showTimeSeparator: true,
),
returnsNormally,
);
expect(
() => CupertinoDatePicker(onDateTimeChanged: (DateTime _) {}, showTimeSeparator: true),
returnsNormally,
);
expect(
() => CupertinoDatePicker(
mode: CupertinoDatePickerMode.date,
onDateTimeChanged: (DateTime _) {},
showTimeSeparator: true,
),
throwsA(
isA<AssertionError>().having(
(AssertionError e) => e.message ?? 'Unknown error',
'message',
contains('showTimeSeparator is only supported in time or dateAndTime modes'),
),
),
);
expect(
() => CupertinoDatePicker(
mode: CupertinoDatePickerMode.monthYear,
onDateTimeChanged: (DateTime _) {},
showTimeSeparator: true,
),
throwsA(
isA<AssertionError>().having(
(AssertionError e) => e.message ?? 'Unknown error',
'message',
contains('showTimeSeparator is only supported in time or dateAndTime modes'),
),
),
);
});
testWidgets('Time separator widget should be rendered when flag is set to true', (
WidgetTester tester,
) async {
await tester.pumpWidget(
CupertinoApp(
home: Center(
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.time,
onDateTimeChanged: (DateTime dateTime) {},
showTimeSeparator: true,
),
),
),
);
expect(find.text(':'), findsOneWidget);
});
testWidgets('Time separator widget should not be rendered when flag is set to false', (
WidgetTester tester,
) async {
await tester.pumpWidget(
CupertinoApp(
home: Center(
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.time,
onDateTimeChanged: (DateTime _) {},
),
),
),
);
expect(find.text(':'), findsNothing);
});
// Regression test for https://github.com/flutter/flutter/issues/161773
testWidgets('CupertinoDatePicker date value baseline alignment', (WidgetTester tester) async {
await tester.pumpWidget(