From 9dc9cf57f2eb2e5592c89df56108aeb8dcec1c9e Mon Sep 17 00:00:00 2001 From: Kirolous Nashaat Date: Tue, 22 Feb 2022 21:45:06 +0200 Subject: [PATCH] Added optional parameter keyboardType to showDatePicker (#93439) --- packages/flutter/lib/src/material/date_picker.dart | 9 +++++++++ .../src/material/input_date_picker_form_field.dart | 8 +++++++- packages/flutter/test/material/date_picker_test.dart | 11 +++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index 3a6294ebae..8ecb641c01 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -151,6 +151,7 @@ Future showDatePicker({ String? errorInvalidText, String? fieldHintText, String? fieldLabelText, + TextInputType? keyboardType, }) async { assert(context != null); assert(initialDate != null); @@ -195,6 +196,7 @@ Future showDatePicker({ errorInvalidText: errorInvalidText, fieldHintText: fieldHintText, fieldLabelText: fieldLabelText, + keyboardType: keyboardType, ); if (textDirection != null) { @@ -249,6 +251,7 @@ class DatePickerDialog extends StatefulWidget { this.errorInvalidText, this.fieldHintText, this.fieldLabelText, + this.keyboardType, this.restorationId, }) : assert(initialDate != null), assert(firstDate != null), @@ -334,6 +337,11 @@ class DatePickerDialog extends StatefulWidget { /// string. For example, 'Month, Day, Year' for en_US. final String? fieldLabelText; + /// The keyboard type of the [TextField]. + /// + /// If this is null, it will default to [TextInputType.datetime] + final TextInputType? keyboardType; + /// Restoration ID to save and restore the state of the [DatePickerDialog]. /// /// If it is non-null, the date picker will persist and restore the @@ -512,6 +520,7 @@ class _DatePickerDialogState extends State with RestorationMix errorInvalidText: widget.errorInvalidText, fieldHintText: widget.fieldHintText, fieldLabelText: widget.fieldLabelText, + keyboardType: widget.keyboardType, autofocus: true, ), const Spacer(), 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 d61a60b66c..8919a0ea2f 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 @@ -56,6 +56,7 @@ class InputDatePickerFormField extends StatefulWidget { this.errorInvalidText, this.fieldHintText, this.fieldLabelText, + this.keyboardType, this.autofocus = false, }) : assert(firstDate != null), assert(lastDate != null), @@ -125,6 +126,11 @@ class InputDatePickerFormField extends StatefulWidget { /// string. For example, 'Month, Day, Year' for en_US. final String? fieldLabelText; + /// The keyboard type of the [TextField]. + /// + /// If this is null, it will default to [TextInputType.datetime] + final TextInputType? keyboardType; + /// {@macro flutter.widgets.editableText.autofocus} final bool autofocus; @@ -242,7 +248,7 @@ class _InputDatePickerFormFieldState extends State { labelText: widget.fieldLabelText ?? localizations.dateInputLabel, ), validator: _validateDate, - keyboardType: TextInputType.datetime, + keyboardType: widget.keyboardType ?? TextInputType.datetime, onSaved: _handleSaved, onFieldSubmitted: _handleSubmitted, autofocus: widget.autofocus, diff --git a/packages/flutter/test/material/date_picker_test.dart b/packages/flutter/test/material/date_picker_test.dart index 055c82e4e1..70cf8119b1 100644 --- a/packages/flutter/test/material/date_picker_test.dart +++ b/packages/flutter/test/material/date_picker_test.dart @@ -26,6 +26,7 @@ void main() { String? fieldHintText; String? fieldLabelText; String? helpText; + TextInputType? keyboardType; final Finder nextMonthIcon = find.byWidgetPredicate((Widget w) => w is IconButton && (w.tooltip?.startsWith('Next month') ?? false)); final Finder previousMonthIcon = find.byWidgetPredicate((Widget w) => w is IconButton && (w.tooltip?.startsWith('Previous month') ?? false)); @@ -52,6 +53,7 @@ void main() { fieldHintText = null; fieldLabelText = null; helpText = null; + keyboardType = null; }); Future prepareDatePicker( @@ -94,6 +96,7 @@ void main() { fieldHintText: fieldHintText, fieldLabelText: fieldLabelText, helpText: helpText, + keyboardType: keyboardType, builder: (BuildContext context, Widget? child) { return Directionality( textDirection: textDirection, @@ -701,6 +704,14 @@ void main() { }); }); + testWidgets('KeyboardType is used', (WidgetTester tester) async { + keyboardType = TextInputType.text; + await prepareDatePicker(tester, (Future date) async { + final TextField field = textField(tester); + expect(field.keyboardType, TextInputType.text); + }); + }); + testWidgets('Initial date is the default', (WidgetTester tester) async { await prepareDatePicker(tester, (Future date) async { await tester.tap(find.text('OK'));