From 2de1af40f396109c067ced9ea49798c686709e0c Mon Sep 17 00:00:00 2001 From: Tirth Date: Mon, 23 Oct 2023 22:45:08 +0530 Subject: [PATCH] Add `InputDatePickerFormField.focusNode` prop (#136673) Adds `focusNode` prop to `InputDatePickerFormField` widget. Fixes #105881 --- .../material/input_date_picker_form_field.dart | 5 +++++ .../input_date_picker_form_field_test.dart | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) 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 5bd0167351..6341360da6 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 @@ -57,6 +57,7 @@ class InputDatePickerFormField extends StatefulWidget { this.keyboardType, this.autofocus = false, this.acceptEmptyDate = false, + this.focusNode, }) : initialDate = initialDate != null ? DateUtils.dateOnly(initialDate) : null, firstDate = DateUtils.dateOnly(firstDate), lastDate = DateUtils.dateOnly(lastDate) { @@ -136,6 +137,9 @@ class InputDatePickerFormField extends StatefulWidget { /// If true, [errorFormatText] is not shown when the date input field is empty. final bool acceptEmptyDate; + /// {@macro flutter.widgets.Focus.focusNode} + final FocusNode? focusNode; + @override State createState() => _InputDatePickerFormFieldState(); } @@ -266,6 +270,7 @@ class _InputDatePickerFormFieldState extends State { onFieldSubmitted: _handleSubmitted, autofocus: widget.autofocus, controller: _controller, + focusNode: widget.focusNode, ); } } 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 4e09c8f7da..582c6b605c 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 @@ -51,6 +51,7 @@ void main() { ThemeData? theme, Iterable>? localizationsDelegates, bool acceptEmptyDate = false, + FocusNode? focusNode, }) { return MaterialApp( theme: theme ?? ThemeData.from(colorScheme: const ColorScheme.light()), @@ -72,6 +73,7 @@ void main() { fieldLabelText: fieldLabelText, autofocus: autofocus, acceptEmptyDate: acceptEmptyDate, + focusNode: focusNode, ), ), ), @@ -378,4 +380,19 @@ void main() { expect(find.text(errorFormatText), findsOneWidget); }); }); + + testWidgets('FocusNode can request focus', (WidgetTester tester) async { + final FocusNode focusNode = FocusNode(); + await tester.pumpWidget(inputDatePickerField( + focusNode: focusNode, + )); + expect((tester.widget(find.byType(TextField)) as TextField).focusNode, focusNode); + expect(focusNode.hasFocus, isFalse); + focusNode.requestFocus(); + await tester.pumpAndSettle(); + expect(focusNode.hasFocus, isTrue); + focusNode.unfocus(); + await tester.pumpAndSettle(); + expect(focusNode.hasFocus, isFalse); + }); }