Use alwaysUse24HourFormat when formatting time of day (#12517)
* alwaysUse24HourFormat in MediaQuery and time picker * docs; dead code * address some comments * MaterialLocalizations.timeOfDayFormat is the single source of 24-hour-formattedness * Make TimePickerDialog private again * wire up MediaQueryData.fromWindow to Window
This commit is contained in:
@@ -141,8 +141,8 @@ class GlobalMaterialLocalizations implements MaterialLocalizations {
|
||||
}
|
||||
|
||||
@override
|
||||
String formatHour(TimeOfDay timeOfDay) {
|
||||
switch (hourFormat(of: timeOfDayFormat)) {
|
||||
String formatHour(TimeOfDay timeOfDay, { bool alwaysUse24HourFormat: false }) {
|
||||
switch (hourFormat(of: timeOfDayFormat(alwaysUse24HourFormat: alwaysUse24HourFormat))) {
|
||||
case HourFormat.HH:
|
||||
return _twoDigitZeroPaddedFormat.format(timeOfDay.hour);
|
||||
case HourFormat.H:
|
||||
@@ -188,7 +188,7 @@ class GlobalMaterialLocalizations implements MaterialLocalizations {
|
||||
}
|
||||
|
||||
@override
|
||||
String formatTimeOfDay(TimeOfDay timeOfDay) {
|
||||
String formatTimeOfDay(TimeOfDay timeOfDay, { bool alwaysUse24HourFormat: false }) {
|
||||
// Not using intl.DateFormat for two reasons:
|
||||
//
|
||||
// - DateFormat supports more formats than our material time picker does,
|
||||
@@ -197,18 +197,20 @@ class GlobalMaterialLocalizations implements MaterialLocalizations {
|
||||
// - DateFormat operates on DateTime, which is sensitive to time eras and
|
||||
// time zones, while here we want to format hour and minute within one day
|
||||
// no matter what date the day falls on.
|
||||
switch (timeOfDayFormat) {
|
||||
final String hour = formatHour(timeOfDay, alwaysUse24HourFormat: alwaysUse24HourFormat);
|
||||
final String minute = formatMinute(timeOfDay);
|
||||
switch (timeOfDayFormat(alwaysUse24HourFormat: alwaysUse24HourFormat)) {
|
||||
case TimeOfDayFormat.h_colon_mm_space_a:
|
||||
return '${formatHour(timeOfDay)}:${formatMinute(timeOfDay)} ${_formatDayPeriod(timeOfDay)}';
|
||||
return '$hour:$minute ${_formatDayPeriod(timeOfDay)}';
|
||||
case TimeOfDayFormat.H_colon_mm:
|
||||
case TimeOfDayFormat.HH_colon_mm:
|
||||
return '${formatHour(timeOfDay)}:${formatMinute(timeOfDay)}';
|
||||
return '$hour:$minute';
|
||||
case TimeOfDayFormat.HH_dot_mm:
|
||||
return '${formatHour(timeOfDay)}.${formatMinute(timeOfDay)}';
|
||||
return '$hour.$minute';
|
||||
case TimeOfDayFormat.a_space_h_colon_mm:
|
||||
return '${_formatDayPeriod(timeOfDay)} ${formatHour(timeOfDay)}:${formatMinute(timeOfDay)}';
|
||||
return '${_formatDayPeriod(timeOfDay)} $hour:$minute';
|
||||
case TimeOfDayFormat.frenchCanadian:
|
||||
return '${formatHour(timeOfDay)} h ${formatMinute(timeOfDay)}';
|
||||
return '$hour h $minute';
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -328,7 +330,7 @@ class GlobalMaterialLocalizations implements MaterialLocalizations {
|
||||
/// * http://demo.icu-project.org/icu-bin/locexp?d_=en&_=en_US shows the
|
||||
/// short time pattern used in locale en_US
|
||||
@override
|
||||
TimeOfDayFormat get timeOfDayFormat {
|
||||
TimeOfDayFormat timeOfDayFormat({ bool alwaysUse24HourFormat: false }) {
|
||||
final String icuShortTimePattern = _nameToValue['timeOfDayFormat'];
|
||||
|
||||
assert(() {
|
||||
@@ -343,7 +345,12 @@ class GlobalMaterialLocalizations implements MaterialLocalizations {
|
||||
return true;
|
||||
}());
|
||||
|
||||
return _icuTimeOfDayToEnum[icuShortTimePattern];
|
||||
final TimeOfDayFormat icuFormat = _icuTimeOfDayToEnum[icuShortTimePattern];
|
||||
|
||||
if (alwaysUse24HourFormat)
|
||||
return _get24HourVersionOf(icuFormat);
|
||||
|
||||
return icuFormat;
|
||||
}
|
||||
|
||||
/// Looks up text geometry defined in [MaterialTextGeometry].
|
||||
@@ -403,6 +410,23 @@ const Map<String, TimeOfDayFormat> _icuTimeOfDayToEnum = const <String, TimeOfDa
|
||||
'ah:mm': TimeOfDayFormat.a_space_h_colon_mm,
|
||||
};
|
||||
|
||||
/// Finds the [TimeOfDayFormat] to use instead of the `original` when the
|
||||
/// `original` uses 12-hour format and [MediaQueryData.alwaysUse24HourFormat]
|
||||
/// is true.
|
||||
TimeOfDayFormat _get24HourVersionOf(TimeOfDayFormat original) {
|
||||
switch (original) {
|
||||
case TimeOfDayFormat.HH_colon_mm:
|
||||
case TimeOfDayFormat.HH_dot_mm:
|
||||
case TimeOfDayFormat.frenchCanadian:
|
||||
case TimeOfDayFormat.H_colon_mm:
|
||||
return original;
|
||||
case TimeOfDayFormat.h_colon_mm_space_a:
|
||||
case TimeOfDayFormat.a_space_h_colon_mm:
|
||||
return TimeOfDayFormat.HH_colon_mm;
|
||||
}
|
||||
return TimeOfDayFormat.HH_colon_mm;
|
||||
}
|
||||
|
||||
/// Tracks if date i18n data has been loaded.
|
||||
bool _dateIntlDataInitialized = false;
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
@@ -24,40 +26,44 @@ void main() {
|
||||
});
|
||||
|
||||
group('formatHour', () {
|
||||
test('formats h', () {
|
||||
GlobalMaterialLocalizations localizations;
|
||||
Future<String> formatHour(WidgetTester tester, Locale locale, TimeOfDay timeOfDay) async {
|
||||
final Completer<String> completer = new Completer<String>();
|
||||
await tester.pumpWidget(new MaterialApp(
|
||||
supportedLocales: <Locale>[locale],
|
||||
locale: locale,
|
||||
localizationsDelegates: <LocalizationsDelegate<dynamic>>[
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
],
|
||||
home: new Builder(builder: (BuildContext context) {
|
||||
completer.complete(MaterialLocalizations.of(context).formatHour(timeOfDay));
|
||||
return new Container();
|
||||
}),
|
||||
));
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('en', 'US'));
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 10, minute: 0)), '10');
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 20, minute: 0)), '8');
|
||||
testWidgets('formats h', (WidgetTester tester) async {
|
||||
expect(await formatHour(tester, const Locale('en', 'US'), const TimeOfDay(hour: 10, minute: 0)), '10');
|
||||
expect(await formatHour(tester, const Locale('en', 'US'), const TimeOfDay(hour: 20, minute: 0)), '8');
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('ar', ''));
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 10, minute: 0)), '١٠');
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 20, minute: 0)), '٨');
|
||||
expect(await formatHour(tester, const Locale('ar', ''), const TimeOfDay(hour: 10, minute: 0)), '١٠');
|
||||
expect(await formatHour(tester, const Locale('ar', ''), const TimeOfDay(hour: 20, minute: 0)), '٨');
|
||||
});
|
||||
|
||||
test('formats HH', () {
|
||||
GlobalMaterialLocalizations localizations;
|
||||
testWidgets('formats HH', (WidgetTester tester) async {
|
||||
expect(await formatHour(tester, const Locale('de', ''), const TimeOfDay(hour: 9, minute: 0)), '09');
|
||||
expect(await formatHour(tester, const Locale('de', ''), const TimeOfDay(hour: 20, minute: 0)), '20');
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('de', ''));
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 9, minute: 0)), '09');
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 20, minute: 0)), '20');
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('en', 'GB'));
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 9, minute: 0)), '09');
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 20, minute: 0)), '20');
|
||||
expect(await formatHour(tester, const Locale('en', 'GB'), const TimeOfDay(hour: 9, minute: 0)), '09');
|
||||
expect(await formatHour(tester, const Locale('en', 'GB'), const TimeOfDay(hour: 20, minute: 0)), '20');
|
||||
});
|
||||
|
||||
test('formats H', () {
|
||||
GlobalMaterialLocalizations localizations;
|
||||
testWidgets('formats H', (WidgetTester tester) async {
|
||||
expect(await formatHour(tester, const Locale('es', ''), const TimeOfDay(hour: 9, minute: 0)), '9');
|
||||
expect(await formatHour(tester, const Locale('es', ''), const TimeOfDay(hour: 20, minute: 0)), '20');
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('es', ''));
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 9, minute: 0)), '9');
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 20, minute: 0)), '20');
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('fa', ''));
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 9, minute: 0)), '۹');
|
||||
expect(localizations.formatHour(const TimeOfDay(hour: 20, minute: 0)), '۲۰');
|
||||
expect(await formatHour(tester, const Locale('fa', ''), const TimeOfDay(hour: 9, minute: 0)), '۹');
|
||||
expect(await formatHour(tester, const Locale('fa', ''), const TimeOfDay(hour: 20, minute: 0)), '۲۰');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -74,48 +80,49 @@ void main() {
|
||||
});
|
||||
|
||||
group('formatTimeOfDay', () {
|
||||
test('formats ${TimeOfDayFormat.h_colon_mm_space_a}', () {
|
||||
GlobalMaterialLocalizations localizations;
|
||||
Future<String> formatTimeOfDay(WidgetTester tester, Locale locale, TimeOfDay timeOfDay) async {
|
||||
final Completer<String> completer = new Completer<String>();
|
||||
await tester.pumpWidget(new MaterialApp(
|
||||
supportedLocales: <Locale>[locale],
|
||||
locale: locale,
|
||||
localizationsDelegates: <LocalizationsDelegate<dynamic>>[
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
],
|
||||
home: new Builder(builder: (BuildContext context) {
|
||||
completer.complete(MaterialLocalizations.of(context).formatTimeOfDay(timeOfDay));
|
||||
return new Container();
|
||||
}),
|
||||
));
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('ar', ''));
|
||||
expect(localizations.formatTimeOfDay(const TimeOfDay(hour: 9, minute: 32)), '٩:٣٢ ص');
|
||||
testWidgets('formats ${TimeOfDayFormat.h_colon_mm_space_a}', (WidgetTester tester) async {
|
||||
expect(await formatTimeOfDay(tester, const Locale('ar', ''), const TimeOfDay(hour: 9, minute: 32)), '٩:٣٢ ص');
|
||||
expect(await formatTimeOfDay(tester, const Locale('ar', ''), const TimeOfDay(hour: 20, minute: 32)), '٨:٣٢ م');
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('en', ''));
|
||||
expect(localizations.formatTimeOfDay(const TimeOfDay(hour: 9, minute: 32)), '9:32 AM');
|
||||
expect(await formatTimeOfDay(tester, const Locale('en', ''), const TimeOfDay(hour: 9, minute: 32)), '9:32 AM');
|
||||
expect(await formatTimeOfDay(tester, const Locale('en', ''), const TimeOfDay(hour: 20, minute: 32)), '8:32 PM');
|
||||
});
|
||||
|
||||
test('formats ${TimeOfDayFormat.HH_colon_mm}', () {
|
||||
GlobalMaterialLocalizations localizations;
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('de', ''));
|
||||
expect(localizations.formatTimeOfDay(const TimeOfDay(hour: 9, minute: 32)), '09:32');
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('en', 'ZA'));
|
||||
expect(localizations.formatTimeOfDay(const TimeOfDay(hour: 9, minute: 32)), '09:32');
|
||||
testWidgets('formats ${TimeOfDayFormat.HH_colon_mm}', (WidgetTester tester) async {
|
||||
expect(await formatTimeOfDay(tester, const Locale('de', ''), const TimeOfDay(hour: 9, minute: 32)), '09:32');
|
||||
expect(await formatTimeOfDay(tester, const Locale('en', 'ZA'), const TimeOfDay(hour: 9, minute: 32)), '09:32');
|
||||
});
|
||||
|
||||
test('formats ${TimeOfDayFormat.H_colon_mm}', () {
|
||||
GlobalMaterialLocalizations localizations;
|
||||
testWidgets('formats ${TimeOfDayFormat.H_colon_mm}', (WidgetTester tester) async {
|
||||
expect(await formatTimeOfDay(tester, const Locale('es', ''), const TimeOfDay(hour: 9, minute: 32)), '9:32');
|
||||
expect(await formatTimeOfDay(tester, const Locale('es', ''), const TimeOfDay(hour: 20, minute: 32)), '20:32');
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('es', ''));
|
||||
expect(localizations.formatTimeOfDay(const TimeOfDay(hour: 9, minute: 32)), '9:32');
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('ja', ''));
|
||||
expect(localizations.formatTimeOfDay(const TimeOfDay(hour: 9, minute: 32)), '9:32');
|
||||
expect(await formatTimeOfDay(tester, const Locale('ja', ''), const TimeOfDay(hour: 9, minute: 32)), '9:32');
|
||||
expect(await formatTimeOfDay(tester, const Locale('ja', ''), const TimeOfDay(hour: 20, minute: 32)), '20:32');
|
||||
});
|
||||
|
||||
test('formats ${TimeOfDayFormat.frenchCanadian}', () {
|
||||
GlobalMaterialLocalizations localizations;
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('fr', 'CA'));
|
||||
expect(localizations.formatTimeOfDay(const TimeOfDay(hour: 9, minute: 32)), '09 h 32');
|
||||
testWidgets('formats ${TimeOfDayFormat.frenchCanadian}', (WidgetTester tester) async {
|
||||
expect(await formatTimeOfDay(tester, const Locale('fr', 'CA'), const TimeOfDay(hour: 9, minute: 32)), '09 h 32');
|
||||
});
|
||||
|
||||
test('formats ${TimeOfDayFormat.a_space_h_colon_mm}', () {
|
||||
GlobalMaterialLocalizations localizations;
|
||||
|
||||
localizations = new GlobalMaterialLocalizations(const Locale('zh', ''));
|
||||
expect(localizations.formatTimeOfDay(const TimeOfDay(hour: 9, minute: 32)), '上午 9:32');
|
||||
testWidgets('formats ${TimeOfDayFormat.a_space_h_colon_mm}', (WidgetTester tester) async {
|
||||
expect(await formatTimeOfDay(tester, const Locale('zh', ''), const TimeOfDay(hour: 9, minute: 32)), '上午 9:32');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -48,8 +48,7 @@ Future<Offset> startPicker(WidgetTester tester, ValueChanged<TimeOfDay> onChange
|
||||
}
|
||||
|
||||
Future<Null> finishPicker(WidgetTester tester) async {
|
||||
final Element timePickerElement = tester.element(find.byElementPredicate((Element element) => element.widget.runtimeType.toString() == '_TimePickerDialog'));
|
||||
final MaterialLocalizations materialLocalizations = MaterialLocalizations.of(timePickerElement);
|
||||
final MaterialLocalizations materialLocalizations = MaterialLocalizations.of(tester.element(find.byType(RaisedButton)));
|
||||
await tester.tap(find.text(materialLocalizations.okButtonLabel));
|
||||
await tester.pumpAndSettle(const Duration(seconds: 1));
|
||||
}
|
||||
@@ -58,11 +57,11 @@ 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 h', 'string :', 'minute', 'period'], //'h:mm a'
|
||||
const Locale('en', 'GB'): const <String>['hour HH', 'string :', 'minute'], //'HH:mm'
|
||||
const Locale('es', 'ES'): const <String>['hour H', 'string :', 'minute'], //'H:mm'
|
||||
const Locale('fr', 'CA'): const <String>['hour HH', 'string h', 'minute'], //'HH \'h\' mm'
|
||||
const Locale('zh', 'ZH'): const <String>['period', 'hour h', 'string :', 'minute'], //'ah:mm'
|
||||
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'
|
||||
};
|
||||
|
||||
for (Locale locale in locales.keys) {
|
||||
@@ -77,7 +76,7 @@ void main() {
|
||||
} else if (fragmentType == '_DayPeriodControl') {
|
||||
actual.add('period');
|
||||
} else if (fragmentType == '_HourControl') {
|
||||
actual.add('hour ${widget.hourFormat.toString().split('.').last}');
|
||||
actual.add('hour');
|
||||
} else if (fragmentType == '_StringFragment') {
|
||||
actual.add('string ${widget.value}');
|
||||
} else {
|
||||
@@ -126,4 +125,72 @@ void main() {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const List<String> labels12To11 = const <String>['12', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'];
|
||||
const List<String> labels12To11TwoDigit = const <String>['12', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11'];
|
||||
const List<String> labels00To23 = const <String>['00', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'];
|
||||
|
||||
Future<Null> mediaQueryBoilerplate(WidgetTester tester, bool alwaysUse24HourFormat) async {
|
||||
await tester.pumpWidget(
|
||||
new Localizations(
|
||||
locale: const Locale('en', 'US'),
|
||||
delegates: <LocalizationsDelegate<dynamic>>[
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
DefaultWidgetsLocalizations.delegate,
|
||||
],
|
||||
child: new MediaQuery(
|
||||
data: new MediaQueryData(alwaysUse24HourFormat: alwaysUse24HourFormat),
|
||||
child: new Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: new Navigator(
|
||||
onGenerateRoute: (RouteSettings settings) {
|
||||
return new MaterialPageRoute<dynamic>(builder: (BuildContext context) {
|
||||
showTimePicker(context: context, initialTime: const TimeOfDay(hour: 7, minute: 0));
|
||||
return new Container();
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
// Pump once, because the dialog shows up asynchronously.
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
testWidgets('respects MediaQueryData.alwaysUse24HourFormat == false', (WidgetTester tester) async {
|
||||
await mediaQueryBoilerplate(tester, false);
|
||||
|
||||
final CustomPaint dialPaint = tester.widget(find.descendant(
|
||||
of: find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_Dial'),
|
||||
matching: find.byType(CustomPaint),
|
||||
));
|
||||
final dynamic dialPainter = dialPaint.painter;
|
||||
final List<TextPainter> primaryOuterLabels = dialPainter.primaryOuterLabels;
|
||||
expect(primaryOuterLabels.map((TextPainter tp) => tp.text.text), labels12To11);
|
||||
expect(dialPainter.primaryInnerLabels, null);
|
||||
|
||||
final List<TextPainter> secondaryOuterLabels = dialPainter.secondaryOuterLabels;
|
||||
expect(secondaryOuterLabels.map((TextPainter tp) => tp.text.text), labels12To11);
|
||||
expect(dialPainter.secondaryInnerLabels, null);
|
||||
});
|
||||
|
||||
testWidgets('respects MediaQueryData.alwaysUse24HourFormat == true', (WidgetTester tester) async {
|
||||
await mediaQueryBoilerplate(tester, true);
|
||||
|
||||
final CustomPaint dialPaint = tester.widget(find.descendant(
|
||||
of: find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_Dial'),
|
||||
matching: find.byType(CustomPaint),
|
||||
));
|
||||
final dynamic dialPainter = dialPaint.painter;
|
||||
final List<TextPainter> primaryOuterLabels = dialPainter.primaryOuterLabels;
|
||||
expect(primaryOuterLabels.map((TextPainter tp) => tp.text.text), labels00To23);
|
||||
final List<TextPainter> primaryInnerLabels = dialPainter.primaryInnerLabels;
|
||||
expect(primaryInnerLabels.map((TextPainter tp) => tp.text.text), labels12To11TwoDigit);
|
||||
|
||||
final List<TextPainter> secondaryOuterLabels = dialPainter.secondaryOuterLabels;
|
||||
expect(secondaryOuterLabels.map((TextPainter tp) => tp.text.text), labels00To23);
|
||||
final List<TextPainter> secondaryInnerLabels = dialPainter.secondaryInnerLabels;
|
||||
expect(secondaryInnerLabels.map((TextPainter tp) => tp.text.text), labels12To11TwoDigit);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user