Skip to content

Commit 3bd8bac

Browse files
authored
[material_ui] Fix for time picker error in locales that do not use Latin digits (flutter#13035)
Port of flutter#186311, fixes flutter#85527 The time picker currently uses `int.parse` and `int.tryParse` to parse the entered number. However, in some locales, numbers are not represented using Latin digits, which causes the parsing to fail. In this implementation, I use `MaterialLocalizations` to parse the number directly, ensuring that it works correctly for all locales. A test has been added to ensure that the new implementation works as expected and does not cause any breaking changes. ## Pre-Review Checklist
1 parent 95c83e5 commit 3bd8bac

3 files changed

Lines changed: 114 additions & 3 deletions

File tree

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

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,18 @@ class _TimePickerInputState extends State<_TimePickerInput> with RestorationMixi
17521752
late final RestorableTimeOfDay _selectedTime = RestorableTimeOfDay(widget.initialSelectedTime);
17531753
final RestorableBool hourHasError = RestorableBool(false);
17541754
final RestorableBool minuteHasError = RestorableBool(false);
1755+
Map<String, int>? _cachedNumbers;
1756+
Locale? _cachedLocale;
1757+
1758+
@override
1759+
void didChangeDependencies() {
1760+
super.didChangeDependencies();
1761+
final Locale locale = Localizations.localeOf(context);
1762+
if (_cachedLocale != locale) {
1763+
_cachedNumbers = null;
1764+
_cachedLocale = locale;
1765+
}
1766+
}
17551767

17561768
@override
17571769
void dispose() {
@@ -1776,7 +1788,7 @@ class _TimePickerInputState extends State<_TimePickerInput> with RestorationMixi
17761788
return null;
17771789
}
17781790

1779-
int? newHour = int.tryParse(value);
1791+
int? newHour = _tryParseLocalizedNumber(value);
17801792
if (newHour == null) {
17811793
return null;
17821794
}
@@ -1802,7 +1814,7 @@ class _TimePickerInputState extends State<_TimePickerInput> with RestorationMixi
18021814
return null;
18031815
}
18041816

1805-
final int? newMinute = int.tryParse(value);
1817+
final int? newMinute = _tryParseLocalizedNumber(value);
18061818
if (newMinute == null) {
18071819
return null;
18081820
}
@@ -1813,6 +1825,36 @@ class _TimePickerInputState extends State<_TimePickerInput> with RestorationMixi
18131825
return null;
18141826
}
18151827

1828+
int? _tryParseLocalizedNumber(String number) {
1829+
final int? parsedNumber = int.tryParse(number);
1830+
if (parsedNumber != null) {
1831+
return parsedNumber;
1832+
}
1833+
1834+
final Map<String, int> cachedNumbers = _cachedNumbers ??= _generateLocalizedNumbers();
1835+
1836+
final String trimmedNumber = _removeLeadingZeros(number);
1837+
1838+
return cachedNumbers[trimmedNumber];
1839+
}
1840+
1841+
Map<String, int> _generateLocalizedNumbers() {
1842+
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
1843+
1844+
// Supports 0 - 59.
1845+
final numbers = <String, int>{for (int i = 0; i < 60; i++) localizations.formatDecimal(i): i};
1846+
1847+
return numbers;
1848+
}
1849+
1850+
String _removeLeadingZeros(String value) {
1851+
final String localizedZero = MaterialLocalizations.of(context).formatDecimal(0);
1852+
while (value.startsWith(localizedZero) && value.length > localizedZero.length) {
1853+
value = value.substring(localizedZero.length);
1854+
}
1855+
return value;
1856+
}
1857+
18161858
void _handleHourSavedSubmitted(String? value) {
18171859
final int? newHour = _parseHour(value);
18181860
if (newHour != null) {
@@ -1833,7 +1875,7 @@ class _TimePickerInputState extends State<_TimePickerInput> with RestorationMixi
18331875
void _handleMinuteSavedSubmitted(String? value) {
18341876
final int? newMinute = _parseMinute(value);
18351877
if (newMinute != null) {
1836-
_selectedTime.value = TimeOfDay(hour: _selectedTime.value.hour, minute: int.parse(value!));
1878+
_selectedTime.value = TimeOfDay(hour: _selectedTime.value.hour, minute: newMinute);
18371879
_TimePickerModel.setSelectedTime(context, _selectedTime.value);
18381880
FocusScope.of(context).unfocus();
18391881
}
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 failing to parse localized digits (e.g. Farsi, Arabic, Bengali, Myanmar, Thai, Nepali, French, German, Portuguese locales) by parsing hour/minute via MaterialLocalizations and handling localized leading zeros.
3+
version: patch

‎packages/material_ui/test/time_picker_test.dart‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,72 @@ void main() {
20962096
expect(result, equals(const TimeOfDay(hour: 9, minute: 12)));
20972097
});
20982098

2099+
testWidgets('parses localized digits in input fields', (WidgetTester tester) async {
2100+
const localizedDigitLocales = <Locale>[
2101+
Locale('en'),
2102+
Locale('ar'),
2103+
Locale('fa'),
2104+
Locale('bn'),
2105+
Locale('my'),
2106+
Locale('th'),
2107+
Locale('ne'),
2108+
Locale('fr'),
2109+
Locale('de'),
2110+
Locale('pt'),
2111+
];
2112+
2113+
Future<void> verifyLocale(Locale locale) async {
2114+
TimeOfDay? result;
2115+
2116+
await tester.pumpWidget(
2117+
MaterialApp(
2118+
localizationsDelegates: GlobalMaterialLocalizations.delegates,
2119+
supportedLocales: <Locale>[const Locale('en'), locale],
2120+
restorationScopeId: 'app',
2121+
locale: locale,
2122+
theme: ThemeData(useMaterial3: materialType == MaterialType.material3),
2123+
home: _TimePickerLauncher(
2124+
onChanged: (TimeOfDay? time) {
2125+
result = time;
2126+
},
2127+
emptyInitialInput: false,
2128+
),
2129+
),
2130+
);
2131+
2132+
// Open the picker.
2133+
await tester.tap(find.text('X'));
2134+
await tester.pumpAndSettle();
2135+
2136+
// Switch to input mode and read the text fields.
2137+
await tester.tap(find.byIcon(Icons.keyboard_outlined));
2138+
await tester.pumpAndSettle();
2139+
2140+
final TextField hourField = tester.widget(find.byType(TextField).first);
2141+
final TextField minuteField = tester.widget(find.byType(TextField).last);
2142+
2143+
final String hourText = hourField.controller?.text ?? '';
2144+
final String minuteText = minuteField.controller?.text ?? '';
2145+
2146+
final BuildContext context = tester.element(find.byType(TextField).first);
2147+
2148+
const timeOfDay = TimeOfDay(hour: 7, minute: 0);
2149+
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
2150+
final String localizedHour = localizations.formatHour(timeOfDay);
2151+
expect(hourText, equals(localizedHour));
2152+
final String localizedMinute = localizations.formatMinute(timeOfDay);
2153+
expect(minuteText, equals(localizedMinute));
2154+
2155+
await finishPicker(tester);
2156+
2157+
expect(result, equals(timeOfDay));
2158+
}
2159+
2160+
for (final locale in localizedDigitLocales) {
2161+
await verifyLocale(locale);
2162+
}
2163+
});
2164+
20992165
testWidgets('Toggle to dial mode keeps selected time', (WidgetTester tester) async {
21002166
late TimeOfDay result;
21012167
await startPicker(

0 commit comments

Comments
 (0)