Unverified Commit 5de716f7 authored by Tirth's avatar Tirth Committed by GitHub

[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) |
parent 3ba249a6
......@@ -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<InputDatePickerFormField> createState() => _InputDatePickerFormFieldState();
}
......@@ -206,6 +214,9 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> {
}
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;
......
......@@ -49,6 +49,7 @@ void main() {
Key? formKey,
ThemeData? theme,
Iterable<LocalizationsDelegate<dynamic>>? 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<FormState> formKey = GlobalKey<FormState>();
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<FormState> formKey = GlobalKey<FormState>();
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);
});
});
}
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