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); + }); }); }