From 5de716f7f850df3117dd78a0026434eaa56554a1 Mon Sep 17 00:00:00 2001 From: Tirth Date: Tue, 18 Apr 2023 15:03:30 +0530 Subject: [PATCH] [InputDatePickerFormField] adds acceptEmptyDate to InputDatePickerFormField Widget (#124143) Adds `acceptEmptyDate` property to `InputDatePickerFormField`. Fixes: #117511 | `acceptEmptyDate` is **false** | `acceptEmptyDate` is **true** | | --- | ---| | ![acceptemptydate_false](https://user-images.githubusercontent.com/13456345/229893658-280ecdee-d509-4579-b53c-9d8d485c61b4.gif) | ![acceptemptydate__true](https://user-images.githubusercontent.com/13456345/229895144-115e71bd-e5bb-4653-8db2-9f57dd8262aa.gif) | --- .../input_date_picker_form_field.dart | 11 +++++++ .../input_date_picker_form_field_test.dart | 31 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/packages/flutter/lib/src/material/input_date_picker_form_field.dart b/packages/flutter/lib/src/material/input_date_picker_form_field.dart index 7c68804525..460ebee72d 100644 --- a/packages/flutter/lib/src/material/input_date_picker_form_field.dart +++ b/packages/flutter/lib/src/material/input_date_picker_form_field.dart @@ -58,6 +58,7 @@ class InputDatePickerFormField extends StatefulWidget { this.fieldLabelText, this.keyboardType, this.autofocus = false, + this.acceptEmptyDate = false, }) : initialDate = initialDate != null ? DateUtils.dateOnly(initialDate) : null, firstDate = DateUtils.dateOnly(firstDate), lastDate = DateUtils.dateOnly(lastDate) { @@ -130,6 +131,13 @@ class InputDatePickerFormField extends StatefulWidget { /// {@macro flutter.widgets.editableText.autofocus} final bool autofocus; + /// Determines if an empty date would show [errorFormatText] or not. + /// + /// Defaults to false. + /// + /// If true, [errorFormatText] is not shown when the date input field is empty. + final bool acceptEmptyDate; + @override State createState() => _InputDatePickerFormFieldState(); } @@ -206,6 +214,9 @@ class _InputDatePickerFormFieldState extends State { } String? _validateDate(String? text) { + if ((text == null || text.isEmpty) && widget.acceptEmptyDate) { + return null; + } final DateTime? date = _parseDate(text); if (date == null) { return widget.errorFormatText ?? MaterialLocalizations.of(context).invalidDateFormatLabel; diff --git a/packages/flutter/test/material/input_date_picker_form_field_test.dart b/packages/flutter/test/material/input_date_picker_form_field_test.dart index a07ac58f41..076b3978f4 100644 --- a/packages/flutter/test/material/input_date_picker_form_field_test.dart +++ b/packages/flutter/test/material/input_date_picker_form_field_test.dart @@ -49,6 +49,7 @@ void main() { Key? formKey, ThemeData? theme, Iterable>? localizationsDelegates, + bool acceptEmptyDate = false, }) { return MaterialApp( theme: theme ?? ThemeData.from(colorScheme: const ColorScheme.light()), @@ -69,6 +70,7 @@ void main() { fieldHintText: fieldHintText, fieldLabelText: fieldLabelText, autofocus: autofocus, + acceptEmptyDate: acceptEmptyDate, ), ), ), @@ -345,5 +347,34 @@ void main() { ) ); }); + + testWidgets('when an empty date is entered and acceptEmptyDate is true, then errorFormatText is not shown', (WidgetTester tester) async { + final GlobalKey formKey = GlobalKey(); + const String errorFormatText = 'That is not a date.'; + await tester.pumpWidget(inputDatePickerField( + errorFormatText: errorFormatText, + formKey: formKey, + acceptEmptyDate: true, + )); + await tester.enterText(find.byType(TextField), ''); + await tester.pumpAndSettle(); + formKey.currentState!.validate(); + await tester.pumpAndSettle(); + expect(find.text(errorFormatText), findsNothing); + }); + + testWidgets('when an empty date is entered and acceptEmptyDate is false, then errorFormatText is shown', (WidgetTester tester) async { + final GlobalKey formKey = GlobalKey(); + const String errorFormatText = 'That is not a date.'; + await tester.pumpWidget(inputDatePickerField( + errorFormatText: errorFormatText, + formKey: formKey, + )); + await tester.enterText(find.byType(TextField), ''); + await tester.pumpAndSettle(); + formKey.currentState!.validate(); + await tester.pumpAndSettle(); + expect(find.text(errorFormatText), findsOneWidget); + }); }); }