diff --git a/packages/flutter/lib/src/material/material_localizations.dart b/packages/flutter/lib/src/material/material_localizations.dart index 804e24017d..d2286a488c 100644 --- a/packages/flutter/lib/src/material/material_localizations.dart +++ b/packages/flutter/lib/src/material/material_localizations.dart @@ -884,7 +884,12 @@ class DefaultMaterialLocalizations implements MaterialLocalizations { if (day == null || day < 1 || day > _getDaysInMonth(year, month)) { return null; } - return DateTime(year, month, day); + + try { + return DateTime(year, month, day); + } on ArgumentError { + return null; + } } @override diff --git a/packages/flutter/test/material/date_picker_test.dart b/packages/flutter/test/material/date_picker_test.dart index 0ff9a5e607..d789b494d8 100644 --- a/packages/flutter/test/material/date_picker_test.dart +++ b/packages/flutter/test/material/date_picker_test.dart @@ -960,6 +960,34 @@ void main() { expect(find.text(errorInvalidText!), findsOneWidget); }); }); + + testWidgets('Invalid entered text shows error on autovalidate', (WidgetTester tester) async { + // This is a regression test for https://github.com/flutter/flutter/issues/126397. + await prepareDatePicker(tester, (Future date) async { + final TextField field = textField(tester); + field.controller!.clear(); + + // Enter some text to trigger autovalidate. + await tester.enterText(find.byType(TextField), 'xyz'); + await tester.tap(find.text('OK')); + await tester.pumpAndSettle(); + + // Invalid format validation error should be shown. + expect(find.text('Invalid format.'), findsOneWidget); + + // Clear the text. + field.controller!.clear(); + + // Enter an invalid date that is too long while autovalidate is still on. + await tester.enterText(find.byType(TextField), '10/05/2023666777889'); + await tester.pump(); + + // Invalid format validation error should be shown. + expect(find.text('Invalid format.'), findsOneWidget); + // Should not throw an exception. + expect(tester.takeException(), null); + }); + }); }); group('Semantics', () { diff --git a/packages/flutter/test/material/localizations_test.dart b/packages/flutter/test/material/localizations_test.dart index 0dc4f7525f..d528cc73da 100644 --- a/packages/flutter/test/material/localizations_test.dart +++ b/packages/flutter/test/material/localizations_test.dart @@ -187,4 +187,25 @@ void main() { expect(MaterialLocalizations.of(localizationsAvailable.currentContext!), isA()); }); + + testWidgets("parseCompactDate doesn't throw an exception on invalid text", (WidgetTester tester) async { + // This is a regression test for https://github.com/flutter/flutter/issues/126397. + final GlobalKey localizations = GlobalKey(); + + await tester.pumpWidget( + MaterialApp( + home: Material( + key: localizations, + child: const SizedBox.expand(), + ), + ), + ); + + final MaterialLocalizations materialLocalizations = MaterialLocalizations.of(localizations.currentContext!); + expect(materialLocalizations.parseCompactDate('10/05/2023'), isNotNull); + expect(tester.takeException(), null); + + expect(materialLocalizations.parseCompactDate('10/05/2023666777889'), null); + expect(tester.takeException(), null); + }); }