[Material] Redesign Time Picker (#59191)
This commit is contained in:
@@ -43,7 +43,7 @@ class _TimePickerLauncher extends StatelessWidget {
|
||||
Future<Offset> startPicker(
|
||||
WidgetTester tester,
|
||||
ValueChanged<TimeOfDay> onChanged, {
|
||||
Locale locale = const Locale('en', 'US'),
|
||||
Locale locale = const Locale('en', 'US'),
|
||||
}) async {
|
||||
await tester.pumpWidget(_TimePickerLauncher(onChanged: onChanged, locale: locale,));
|
||||
await tester.tap(find.text('X'));
|
||||
@@ -58,66 +58,151 @@ Future<void> finishPicker(WidgetTester tester) async {
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('can localize the header in all known formats', (WidgetTester tester) async {
|
||||
// TODO(yjbanov): also test `HH.mm` (in_ID), `a h:mm` (ko_KR) and `HH:mm น.` (th_TH) when we have .arb files for them
|
||||
final Map<Locale, List<String>> locales = <Locale, List<String>>{
|
||||
const Locale('en', 'US'): const <String>['hour', 'string :', 'minute', 'period'], //'h:mm a'
|
||||
const Locale('en', 'GB'): const <String>['hour', 'string :', 'minute'], //'HH:mm'
|
||||
const Locale('es', 'ES'): const <String>['hour', 'string :', 'minute'], //'H:mm'
|
||||
const Locale('fr', 'CA'): const <String>['hour', 'string h', 'minute'], //'HH \'h\' mm'
|
||||
const Locale('zh', 'ZH'): const <String>['period', 'hour', 'string :', 'minute'], //'ah:mm'
|
||||
};
|
||||
testWidgets('can localize the header in all known formats - portrait', (WidgetTester tester) async {
|
||||
// Ensure picker is displayed in portrait mode.
|
||||
tester.binding.window.physicalSizeTestValue = const Size(400, 800);
|
||||
tester.binding.window.devicePixelRatioTestValue = 1;
|
||||
|
||||
for (final Locale locale in locales.keys) {
|
||||
final Finder stringFragmentTextFinder = find.descendant(
|
||||
of: find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_StringFragment'),
|
||||
matching: find.byType(Text),
|
||||
).first;
|
||||
final Finder hourControlFinder = find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_HourControl');
|
||||
final Finder minuteControlFinder = find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_MinuteControl');
|
||||
final Finder dayPeriodControlFinder = find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_DayPeriodControl');
|
||||
|
||||
// TODO(yjbanov): also test `HH.mm` (in_ID), `a h:mm` (ko_KR) and `HH:mm น.` (th_TH) when we have .arb files for them
|
||||
final List<Locale> locales = <Locale>[
|
||||
const Locale('en', 'US'), //'h:mm a'
|
||||
const Locale('en', 'GB'), //'HH:mm'
|
||||
const Locale('es', 'ES'), //'H:mm'
|
||||
const Locale('fr', 'CA'), //'HH \'h\' mm'
|
||||
const Locale('zh', 'ZH'), //'ah:mm'
|
||||
];
|
||||
|
||||
for (final Locale locale in locales) {
|
||||
final Offset center = await startPicker(tester, (TimeOfDay time) { }, locale: locale);
|
||||
final List<String> actual = <String>[];
|
||||
tester.element(find.byType(CustomMultiChildLayout)).visitChildren((Element child) {
|
||||
final LayoutId layout = child.widget as LayoutId;
|
||||
final String fragmentType = '${layout.child.runtimeType}';
|
||||
final dynamic widget = layout.child;
|
||||
if (fragmentType == '_MinuteControl') {
|
||||
actual.add('minute');
|
||||
} else if (fragmentType == '_DayPeriodControl') {
|
||||
actual.add('period');
|
||||
} else if (fragmentType == '_HourControl') {
|
||||
actual.add('hour');
|
||||
} else if (fragmentType == '_StringFragment') {
|
||||
actual.add('string ${widget.value}');
|
||||
} else {
|
||||
fail('Unsupported fragment type: $fragmentType');
|
||||
}
|
||||
});
|
||||
expect(actual, locales[locale]);
|
||||
final Text stringFragmentText = tester.widget(stringFragmentTextFinder);
|
||||
final double hourLeftOffset = tester.getTopLeft(hourControlFinder).dx;
|
||||
final double minuteLeftOffset = tester.getTopLeft(minuteControlFinder).dx;
|
||||
final double stringFragmentLeftOffset = tester.getTopLeft(stringFragmentTextFinder).dx;
|
||||
|
||||
if (locale == const Locale('en', 'US')) {
|
||||
final double dayPeriodLeftOffset = tester.getTopLeft(dayPeriodControlFinder).dx;
|
||||
expect(stringFragmentText.data, ':');
|
||||
expect(hourLeftOffset, lessThan(stringFragmentLeftOffset));
|
||||
expect(stringFragmentLeftOffset, lessThan(minuteLeftOffset));
|
||||
expect(minuteLeftOffset, lessThan(dayPeriodLeftOffset));
|
||||
} else if (locale == const Locale('en', 'GB')) {
|
||||
expect(stringFragmentText.data, ':');
|
||||
expect(hourLeftOffset, lessThan(stringFragmentLeftOffset));
|
||||
expect(stringFragmentLeftOffset, lessThan(minuteLeftOffset));
|
||||
expect(dayPeriodControlFinder, findsNothing);
|
||||
} else if (locale == const Locale('es', 'ES')) {
|
||||
expect(stringFragmentText.data, ':');
|
||||
expect(hourLeftOffset, lessThan(stringFragmentLeftOffset));
|
||||
expect(stringFragmentLeftOffset, lessThan(minuteLeftOffset));
|
||||
expect(dayPeriodControlFinder, findsNothing);
|
||||
} else if (locale == const Locale('fr', 'CA')) {
|
||||
expect(stringFragmentText.data, 'h');
|
||||
expect(hourLeftOffset, lessThan(stringFragmentLeftOffset));
|
||||
expect(stringFragmentLeftOffset, lessThan(minuteLeftOffset));
|
||||
expect(dayPeriodControlFinder, findsNothing);
|
||||
} else if (locale == const Locale('zh', 'ZH')) {
|
||||
final double dayPeriodLeftOffset = tester.getTopLeft(dayPeriodControlFinder).dx;
|
||||
expect(stringFragmentText.data, ':');
|
||||
expect(dayPeriodLeftOffset, lessThan(hourLeftOffset));
|
||||
expect(hourLeftOffset, lessThan(stringFragmentLeftOffset));
|
||||
expect(stringFragmentLeftOffset, lessThan(minuteLeftOffset));
|
||||
}
|
||||
await tester.tapAt(Offset(center.dx, center.dy - 50.0));
|
||||
await finishPicker(tester);
|
||||
}
|
||||
|
||||
tester.binding.window.physicalSizeTestValue = null;
|
||||
tester.binding.window.devicePixelRatioTestValue = null;
|
||||
});
|
||||
|
||||
testWidgets('uses single-ring 12-hour dial for h hour format', (WidgetTester tester) async {
|
||||
// Tap along the segment stretching from the center to the edge at
|
||||
// 12:00 AM position. Because there's only one ring, no matter where you
|
||||
// tap the time will be the same. See the 24-hour dial test that behaves
|
||||
// differently.
|
||||
for (int i = 1; i < 10; i++) {
|
||||
TimeOfDay result;
|
||||
final Offset center = await startPicker(tester, (TimeOfDay time) { result = time; });
|
||||
final Size size = tester.getSize(find.byKey(const Key('time-picker-dial')));
|
||||
final double dy = (size.height / 2.0 / 10) * i;
|
||||
await tester.tapAt(Offset(center.dx, center.dy - dy));
|
||||
testWidgets('can localize the header in all known formats - landscape', (WidgetTester tester) async {
|
||||
// Ensure picker is displayed in landscape mode.
|
||||
tester.binding.window.physicalSizeTestValue = const Size(800, 400);
|
||||
tester.binding.window.devicePixelRatioTestValue = 1;
|
||||
|
||||
final Finder stringFragmentTextFinder = find.descendant(
|
||||
of: find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_StringFragment'),
|
||||
matching: find.byType(Text),
|
||||
).first;
|
||||
final Finder hourControlFinder = find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_HourControl');
|
||||
final Finder minuteControlFinder = find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_MinuteControl');
|
||||
final Finder dayPeriodControlFinder = find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_DayPeriodControl');
|
||||
|
||||
// TODO(yjbanov): also test `HH.mm` (in_ID), `a h:mm` (ko_KR) and `HH:mm น.` (th_TH) when we have .arb files for them
|
||||
final List<Locale> locales = <Locale>[
|
||||
const Locale('en', 'US'), //'h:mm a'
|
||||
const Locale('en', 'GB'), //'HH:mm'
|
||||
const Locale('es', 'ES'), //'H:mm'
|
||||
const Locale('fr', 'CA'), //'HH \'h\' mm'
|
||||
const Locale('zh', 'ZH'), //'ah:mm'
|
||||
];
|
||||
|
||||
for (final Locale locale in locales) {
|
||||
final Offset center = await startPicker(tester, (TimeOfDay time) { }, locale: locale);
|
||||
final Text stringFragmentText = tester.widget(stringFragmentTextFinder);
|
||||
final double hourLeftOffset = tester.getTopLeft(hourControlFinder).dx;
|
||||
final double hourTopOffset = tester.getTopLeft(hourControlFinder).dy;
|
||||
final double minuteLeftOffset = tester.getTopLeft(minuteControlFinder).dx;
|
||||
final double stringFragmentLeftOffset = tester.getTopLeft(stringFragmentTextFinder).dx;
|
||||
|
||||
if (locale == const Locale('en', 'US')) {
|
||||
final double dayPeriodLeftOffset = tester.getTopLeft(dayPeriodControlFinder).dx;
|
||||
final double dayPeriodTopOffset = tester.getTopLeft(dayPeriodControlFinder).dy;
|
||||
expect(stringFragmentText.data, ':');
|
||||
expect(hourLeftOffset, lessThan(stringFragmentLeftOffset));
|
||||
expect(stringFragmentLeftOffset, lessThan(minuteLeftOffset));
|
||||
expect(hourLeftOffset, dayPeriodLeftOffset);
|
||||
expect(hourTopOffset, lessThan(dayPeriodTopOffset));
|
||||
} else if (locale == const Locale('en', 'GB')) {
|
||||
expect(stringFragmentText.data, ':');
|
||||
expect(hourLeftOffset, lessThan(stringFragmentLeftOffset));
|
||||
expect(stringFragmentLeftOffset, lessThan(minuteLeftOffset));
|
||||
expect(dayPeriodControlFinder, findsNothing);
|
||||
} else if (locale == const Locale('es', 'ES')) {
|
||||
expect(stringFragmentText.data, ':');
|
||||
expect(hourLeftOffset, lessThan(stringFragmentLeftOffset));
|
||||
expect(stringFragmentLeftOffset, lessThan(minuteLeftOffset));
|
||||
expect(dayPeriodControlFinder, findsNothing);
|
||||
} else if (locale == const Locale('fr', 'CA')) {
|
||||
expect(stringFragmentText.data, 'h');
|
||||
expect(hourLeftOffset, lessThan(stringFragmentLeftOffset));
|
||||
expect(stringFragmentLeftOffset, lessThan(minuteLeftOffset));
|
||||
expect(dayPeriodControlFinder, findsNothing);
|
||||
} else if (locale == const Locale('zh', 'ZH')) {
|
||||
final double dayPeriodLeftOffset = tester.getTopLeft(dayPeriodControlFinder).dx;
|
||||
final double dayPeriodTopOffset = tester.getTopLeft(dayPeriodControlFinder).dy;
|
||||
expect(stringFragmentText.data, ':');
|
||||
expect(hourLeftOffset, lessThan(stringFragmentLeftOffset));
|
||||
expect(stringFragmentLeftOffset, lessThan(minuteLeftOffset));
|
||||
expect(hourLeftOffset, dayPeriodLeftOffset);
|
||||
expect(hourTopOffset, greaterThan(dayPeriodTopOffset));
|
||||
}
|
||||
await tester.tapAt(Offset(center.dx, center.dy - 50.0));
|
||||
await finishPicker(tester);
|
||||
expect(result, equals(const TimeOfDay(hour: 0, minute: 0)));
|
||||
}
|
||||
|
||||
tester.binding.window.physicalSizeTestValue = null;
|
||||
tester.binding.window.devicePixelRatioTestValue = null;
|
||||
});
|
||||
|
||||
testWidgets('uses two-ring 24-hour dial for H and HH hour formats', (WidgetTester tester) async {
|
||||
testWidgets('uses single-ring 24-hour dial for all formats', (WidgetTester tester) async {
|
||||
const List<Locale> locales = <Locale>[
|
||||
Locale('en', 'US'), // h
|
||||
Locale('en', 'GB'), // HH
|
||||
Locale('es', 'ES'), // H
|
||||
];
|
||||
for (final Locale locale in locales) {
|
||||
// Tap along the segment stretching from the center to the edge at
|
||||
// 12:00 AM position. There are two rings. At ~70% mark, the ring
|
||||
// switches between inner ring and outer ring.
|
||||
// 12:00 AM position. Because there's only one ring, no matter where you
|
||||
// tap the time will be the same.
|
||||
for (int i = 1; i < 10; i++) {
|
||||
TimeOfDay result;
|
||||
final Offset center = await startPicker(tester, (TimeOfDay time) { result = time; }, locale: locale);
|
||||
@@ -125,14 +210,13 @@ void main() {
|
||||
final double dy = (size.height / 2.0 / 10) * i;
|
||||
await tester.tapAt(Offset(center.dx, center.dy - dy));
|
||||
await finishPicker(tester);
|
||||
expect(result, equals(TimeOfDay(hour: i < 7 ? 12 : 0, minute: 0)));
|
||||
expect(result, equals(const TimeOfDay(hour: 0, minute: 0)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const List<String> labels12To11 = <String>['12', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'];
|
||||
const List<String> labels12To11TwoDigit = <String>['12', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11'];
|
||||
const List<String> labels00To23 = <String>['00', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'];
|
||||
const List<String> labels00To22TwoDigit = <String>['00', '02', '04', '06', '08', '10', '12', '14', '16', '18', '20', '22'];
|
||||
|
||||
Future<void> mediaQueryBoilerplate(WidgetTester tester, bool alwaysUse24HourFormat) async {
|
||||
await tester.pumpWidget(
|
||||
@@ -174,19 +258,17 @@ void main() {
|
||||
|
||||
final CustomPaint dialPaint = tester.widget(find.byKey(const ValueKey<String>('time-picker-dial')));
|
||||
final dynamic dialPainter = dialPaint.painter;
|
||||
final List<dynamic> primaryOuterLabels = dialPainter.primaryOuterLabels as List<dynamic>;
|
||||
final List<dynamic> primaryLabels = dialPainter.primaryLabels as List<dynamic>;
|
||||
expect(
|
||||
primaryOuterLabels.map<String>((dynamic tp) => ((tp.painter as TextPainter).text as TextSpan).text),
|
||||
primaryLabels.map<String>((dynamic tp) => ((tp.painter as TextPainter).text as TextSpan).text),
|
||||
labels12To11,
|
||||
);
|
||||
expect(dialPainter.primaryInnerLabels, null);
|
||||
|
||||
final List<dynamic> secondaryOuterLabels = dialPainter.secondaryOuterLabels as List<dynamic>;
|
||||
final List<dynamic> secondaryLabels = dialPainter.secondaryLabels as List<dynamic>;
|
||||
expect(
|
||||
secondaryOuterLabels.map<String>((dynamic tp) => ((tp.painter as TextPainter).text as TextSpan).text),
|
||||
secondaryLabels.map<String>((dynamic tp) => ((tp.painter as TextPainter).text as TextSpan).text),
|
||||
labels12To11,
|
||||
);
|
||||
expect(dialPainter.secondaryInnerLabels, null);
|
||||
});
|
||||
|
||||
testWidgets('respects MediaQueryData.alwaysUse24HourFormat == true', (WidgetTester tester) async {
|
||||
@@ -194,26 +276,16 @@ void main() {
|
||||
|
||||
final CustomPaint dialPaint = tester.widget(find.byKey(const ValueKey<String>('time-picker-dial')));
|
||||
final dynamic dialPainter = dialPaint.painter;
|
||||
final List<dynamic> primaryOuterLabels = dialPainter.primaryOuterLabels as List<dynamic>;
|
||||
final List<dynamic> primaryLabels = dialPainter.primaryLabels as List<dynamic>;
|
||||
expect(
|
||||
primaryOuterLabels.map<String>((dynamic tp) => ((tp.painter as TextPainter).text as TextSpan).text),
|
||||
labels00To23,
|
||||
);
|
||||
final List<dynamic> primaryInnerLabels = dialPainter.primaryInnerLabels as List<dynamic>;
|
||||
expect(
|
||||
primaryInnerLabels.map<String>((dynamic tp) => ((tp.painter as TextPainter).text as TextSpan).text),
|
||||
labels12To11TwoDigit,
|
||||
primaryLabels.map<String>((dynamic tp) => ((tp.painter as TextPainter).text as TextSpan).text),
|
||||
labels00To22TwoDigit,
|
||||
);
|
||||
|
||||
final List<dynamic> secondaryOuterLabels = dialPainter.secondaryOuterLabels as List<dynamic>;
|
||||
final List<dynamic> secondaryLabels = dialPainter.secondaryLabels as List<dynamic>;
|
||||
expect(
|
||||
secondaryOuterLabels.map<String>((dynamic tp) => ((tp.painter as TextPainter).text as TextSpan).text),
|
||||
labels00To23,
|
||||
);
|
||||
final List<dynamic> secondaryInnerLabels = dialPainter.secondaryInnerLabels as List<dynamic>;
|
||||
expect(
|
||||
secondaryInnerLabels.map<String>((dynamic tp) => ((tp.painter as TextPainter).text as TextSpan).text),
|
||||
labels12To11TwoDigit,
|
||||
secondaryLabels.map<String>((dynamic tp) => ((tp.painter as TextPainter).text as TextSpan).text),
|
||||
labels00To22TwoDigit,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user