[InputDatePickerFormField] adds acceptEmptyDate to InputDatePickerFormField Widget (#124143)
Adds `acceptEmptyDate` property to `InputDatePickerFormField`. Fixes: #117511 | `acceptEmptyDate` is **false** | `acceptEmptyDate` is **true** | | --- | ---| |  |  |
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user