Unverified Commit 15a7e073 authored by Taha Tesser's avatar Taha Tesser Committed by GitHub

Fix `showDatePicker` in input mode throws an `ArgumentError` (#126525)

fixes https://github.com/flutter/flutter/issues/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.
parent 468512e0
......@@ -884,7 +884,12 @@ class DefaultMaterialLocalizations implements MaterialLocalizations {
if (day == null || day < 1 || day > _getDaysInMonth(year, month)) {
return null;
}
try {
return DateTime(year, month, day);
} on ArgumentError {
return null;
}
}
@override
......
......@@ -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<DateTime?> 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', () {
......
......@@ -187,4 +187,25 @@ void main() {
expect(MaterialLocalizations.of(localizationsAvailable.currentContext!), isA<MaterialLocalizations>());
});
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);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment