Skip to content

Commit 15a7e07

Browse files
authored
Fix showDatePicker in input mode throws an ArgumentError (#126525)
fixes flutter/flutter#126397 The `parseCompactDate` method in `material_localizations.dart` with long text throws an argument error before the date picker could handle it to show a validation error while the date picker is on autovalidate. This PR adds `try/catch` in the `parseCompactDate` to return null on the argument error.
1 parent 468512e commit 15a7e07

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

packages/flutter/lib/src/material/material_localizations.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,12 @@ class DefaultMaterialLocalizations implements MaterialLocalizations {
884884
if (day == null || day < 1 || day > _getDaysInMonth(year, month)) {
885885
return null;
886886
}
887-
return DateTime(year, month, day);
887+
888+
try {
889+
return DateTime(year, month, day);
890+
} on ArgumentError {
891+
return null;
892+
}
888893
}
889894

890895
@override

packages/flutter/test/material/date_picker_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,34 @@ void main() {
960960
expect(find.text(errorInvalidText!), findsOneWidget);
961961
});
962962
});
963+
964+
testWidgets('Invalid entered text shows error on autovalidate', (WidgetTester tester) async {
965+
// This is a regression test for https://github.com/flutter/flutter/issues/126397.
966+
await prepareDatePicker(tester, (Future<DateTime?> date) async {
967+
final TextField field = textField(tester);
968+
field.controller!.clear();
969+
970+
// Enter some text to trigger autovalidate.
971+
await tester.enterText(find.byType(TextField), 'xyz');
972+
await tester.tap(find.text('OK'));
973+
await tester.pumpAndSettle();
974+
975+
// Invalid format validation error should be shown.
976+
expect(find.text('Invalid format.'), findsOneWidget);
977+
978+
// Clear the text.
979+
field.controller!.clear();
980+
981+
// Enter an invalid date that is too long while autovalidate is still on.
982+
await tester.enterText(find.byType(TextField), '10/05/2023666777889');
983+
await tester.pump();
984+
985+
// Invalid format validation error should be shown.
986+
expect(find.text('Invalid format.'), findsOneWidget);
987+
// Should not throw an exception.
988+
expect(tester.takeException(), null);
989+
});
990+
});
963991
});
964992

965993
group('Semantics', () {

packages/flutter/test/material/localizations_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,25 @@ void main() {
187187

188188
expect(MaterialLocalizations.of(localizationsAvailable.currentContext!), isA<MaterialLocalizations>());
189189
});
190+
191+
testWidgets("parseCompactDate doesn't throw an exception on invalid text", (WidgetTester tester) async {
192+
// This is a regression test for https://github.com/flutter/flutter/issues/126397.
193+
final GlobalKey localizations = GlobalKey();
194+
195+
await tester.pumpWidget(
196+
MaterialApp(
197+
home: Material(
198+
key: localizations,
199+
child: const SizedBox.expand(),
200+
),
201+
),
202+
);
203+
204+
final MaterialLocalizations materialLocalizations = MaterialLocalizations.of(localizations.currentContext!);
205+
expect(materialLocalizations.parseCompactDate('10/05/2023'), isNotNull);
206+
expect(tester.takeException(), null);
207+
208+
expect(materialLocalizations.parseCompactDate('10/05/2023666777889'), null);
209+
expect(tester.takeException(), null);
210+
});
190211
}

0 commit comments

Comments
 (0)