Skip to content

Commit 6968b24

Browse files
authored
[material_ui] Fix time picker input validation for 24-hour locales (#13033)
In input mode, `showTimePicker` and `TimePickerDialog` parsed the hour as a 12-hour value when the 24-hour format came from the locale (e.g. en_GB), even though the input UI showed 24-hour fields without an AM/PM selector. As a result, hours 0 and 13–23 were rejected, and hours 1–12 could be shifted by 12 hours depending on the AM/PM period of the selected time. This PR makes the hour parser use the same hour format as the rest of the picker, which already takes the locale into account. Fixes flutter/flutter#74018 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 382004f commit 6968b24

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎packages/material_ui/lib/src/time_picker.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,7 @@ class _TimePickerInputState extends State<_TimePickerInput> with RestorationMixi
17811781
return null;
17821782
}
17831783

1784-
if (MediaQuery.alwaysUse24HourFormatOf(context)) {
1784+
if (_TimePickerModel.hourDialTypeOf(context) != _HourDialType.twelveHour) {
17851785
if (newHour >= 0 && newHour < 24) {
17861786
return newHour;
17871787
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes showTimePicker input mode rejecting or misreading hours in locales that use a 24-hour format.
3+
version: patch

‎packages/material_ui/test/time_picker_test.dart‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,56 @@ void main() {
21422142
expect(result, equals(const TimeOfDay(hour: 8, minute: 15)));
21432143
});
21442144

2145+
testWidgets('Accepts 24-hour input for a 24-hour locale', (WidgetTester tester) async {
2146+
// Regression test for https://github.com/flutter/flutter/issues/74018
2147+
//
2148+
// en_GB uses a 24-hour format (HH:mm), so the hour input must be
2149+
// parsed as a 24-hour value.
2150+
TimeOfDay? result;
2151+
await startPicker(
2152+
tester,
2153+
(TimeOfDay? time) {
2154+
result = time;
2155+
},
2156+
entryMode: TimePickerEntryMode.input,
2157+
materialType: materialType,
2158+
locale: const Locale('en', 'GB'),
2159+
// Make the 24-hour format come from the locale alone. The issue does
2160+
// not occur when alwaysUse24HourFormat is true.
2161+
alwaysUse24HourFormat: false,
2162+
);
2163+
2164+
Future<void> openPicker() async {
2165+
await tester.tap(find.text('X'));
2166+
await tester.pumpAndSettle();
2167+
}
2168+
2169+
Future<void> submitHour(String hour) async {
2170+
await tester.enterText(find.byType(TextField).first, hour);
2171+
await tester.enterText(find.byType(TextField).last, '15');
2172+
await finishPicker(tester);
2173+
}
2174+
2175+
// Hours from 0 to 23 are accepted as is. 0 and 13 to 23 used to be
2176+
// rejected, and 12 used to be shifted to 0 because the initial time
2177+
// (07:00) is in the AM period.
2178+
await submitHour('00');
2179+
expect(result, const TimeOfDay(hour: 0, minute: 15));
2180+
await openPicker();
2181+
await submitHour('12');
2182+
expect(result, const TimeOfDay(hour: 12, minute: 15));
2183+
await openPicker();
2184+
await submitHour('23');
2185+
expect(result, const TimeOfDay(hour: 23, minute: 15));
2186+
2187+
// Hours outside 0 to 23 are rejected and keep the dialog open.
2188+
await openPicker();
2189+
await submitHour('-1');
2190+
expect(find.byType(TimePickerDialog), findsOneWidget);
2191+
await submitHour('24');
2192+
expect(find.byType(TimePickerDialog), findsOneWidget);
2193+
});
2194+
21452195
// Fixes regression that was reverted in https://github.com/flutter/flutter/pull/64094#pullrequestreview-469836378.
21462196
testWidgets('Ensure hour/minute fields are top-aligned with the separator', (
21472197
WidgetTester tester,
@@ -2875,11 +2925,24 @@ Future<Offset?> startPicker(
28752925
String? cancelText,
28762926
String? confirmText,
28772927
bool emptyInitialInput = false,
2928+
Locale? locale,
2929+
bool? alwaysUse24HourFormat,
28782930
}) async {
28792931
await tester.pumpWidget(
28802932
MaterialApp(
28812933
theme: theme ?? ThemeData(useMaterial3: materialType == MaterialType.material3),
28822934
restorationScopeId: 'app',
2935+
locale: locale,
2936+
supportedLocales: locale == null ? const <Locale>[Locale('en', 'US')] : <Locale>[locale],
2937+
localizationsDelegates: locale == null ? null : GlobalMaterialLocalizations.delegates,
2938+
builder: alwaysUse24HourFormat == null
2939+
? null
2940+
: (BuildContext context, Widget? child) {
2941+
return MediaQuery(
2942+
data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: alwaysUse24HourFormat),
2943+
child: child!,
2944+
);
2945+
},
28832946
home: _TimePickerLauncher(
28842947
onChanged: onChanged,
28852948
entryMode: entryMode,

0 commit comments

Comments
 (0)