Skip to content

Commit 5de716f

Browse files
authored
[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) |
1 parent 3ba249a commit 5de716f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class InputDatePickerFormField extends StatefulWidget {
5858
this.fieldLabelText,
5959
this.keyboardType,
6060
this.autofocus = false,
61+
this.acceptEmptyDate = false,
6162
}) : initialDate = initialDate != null ? DateUtils.dateOnly(initialDate) : null,
6263
firstDate = DateUtils.dateOnly(firstDate),
6364
lastDate = DateUtils.dateOnly(lastDate) {
@@ -130,6 +131,13 @@ class InputDatePickerFormField extends StatefulWidget {
130131
/// {@macro flutter.widgets.editableText.autofocus}
131132
final bool autofocus;
132133

134+
/// Determines if an empty date would show [errorFormatText] or not.
135+
///
136+
/// Defaults to false.
137+
///
138+
/// If true, [errorFormatText] is not shown when the date input field is empty.
139+
final bool acceptEmptyDate;
140+
133141
@override
134142
State<InputDatePickerFormField> createState() => _InputDatePickerFormFieldState();
135143
}
@@ -206,6 +214,9 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> {
206214
}
207215

208216
String? _validateDate(String? text) {
217+
if ((text == null || text.isEmpty) && widget.acceptEmptyDate) {
218+
return null;
219+
}
209220
final DateTime? date = _parseDate(text);
210221
if (date == null) {
211222
return widget.errorFormatText ?? MaterialLocalizations.of(context).invalidDateFormatLabel;

packages/flutter/test/material/input_date_picker_form_field_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void main() {
4949
Key? formKey,
5050
ThemeData? theme,
5151
Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates,
52+
bool acceptEmptyDate = false,
5253
}) {
5354
return MaterialApp(
5455
theme: theme ?? ThemeData.from(colorScheme: const ColorScheme.light()),
@@ -69,6 +70,7 @@ void main() {
6970
fieldHintText: fieldHintText,
7071
fieldLabelText: fieldLabelText,
7172
autofocus: autofocus,
73+
acceptEmptyDate: acceptEmptyDate,
7274
),
7375
),
7476
),
@@ -345,5 +347,34 @@ void main() {
345347
)
346348
);
347349
});
350+
351+
testWidgets('when an empty date is entered and acceptEmptyDate is true, then errorFormatText is not shown', (WidgetTester tester) async {
352+
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
353+
const String errorFormatText = 'That is not a date.';
354+
await tester.pumpWidget(inputDatePickerField(
355+
errorFormatText: errorFormatText,
356+
formKey: formKey,
357+
acceptEmptyDate: true,
358+
));
359+
await tester.enterText(find.byType(TextField), '');
360+
await tester.pumpAndSettle();
361+
formKey.currentState!.validate();
362+
await tester.pumpAndSettle();
363+
expect(find.text(errorFormatText), findsNothing);
364+
});
365+
366+
testWidgets('when an empty date is entered and acceptEmptyDate is false, then errorFormatText is shown', (WidgetTester tester) async {
367+
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
368+
const String errorFormatText = 'That is not a date.';
369+
await tester.pumpWidget(inputDatePickerField(
370+
errorFormatText: errorFormatText,
371+
formKey: formKey,
372+
));
373+
await tester.enterText(find.byType(TextField), '');
374+
await tester.pumpAndSettle();
375+
formKey.currentState!.validate();
376+
await tester.pumpAndSettle();
377+
expect(find.text(errorFormatText), findsOneWidget);
378+
});
348379
});
349380
}

0 commit comments

Comments
 (0)