Skip to content

Commit 2de1af4

Browse files
authored
Add InputDatePickerFormField.focusNode prop (flutter#136673)
Adds `focusNode` prop to `InputDatePickerFormField` widget. Fixes flutter#105881
1 parent aea5621 commit 2de1af4

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class InputDatePickerFormField extends StatefulWidget {
5757
this.keyboardType,
5858
this.autofocus = false,
5959
this.acceptEmptyDate = false,
60+
this.focusNode,
6061
}) : initialDate = initialDate != null ? DateUtils.dateOnly(initialDate) : null,
6162
firstDate = DateUtils.dateOnly(firstDate),
6263
lastDate = DateUtils.dateOnly(lastDate) {
@@ -136,6 +137,9 @@ class InputDatePickerFormField extends StatefulWidget {
136137
/// If true, [errorFormatText] is not shown when the date input field is empty.
137138
final bool acceptEmptyDate;
138139

140+
/// {@macro flutter.widgets.Focus.focusNode}
141+
final FocusNode? focusNode;
142+
139143
@override
140144
State<InputDatePickerFormField> createState() => _InputDatePickerFormFieldState();
141145
}
@@ -266,6 +270,7 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> {
266270
onFieldSubmitted: _handleSubmitted,
267271
autofocus: widget.autofocus,
268272
controller: _controller,
273+
focusNode: widget.focusNode,
269274
);
270275
}
271276
}

packages/flutter/test/material/input_date_picker_form_field_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void main() {
5151
ThemeData? theme,
5252
Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates,
5353
bool acceptEmptyDate = false,
54+
FocusNode? focusNode,
5455
}) {
5556
return MaterialApp(
5657
theme: theme ?? ThemeData.from(colorScheme: const ColorScheme.light()),
@@ -72,6 +73,7 @@ void main() {
7273
fieldLabelText: fieldLabelText,
7374
autofocus: autofocus,
7475
acceptEmptyDate: acceptEmptyDate,
76+
focusNode: focusNode,
7577
),
7678
),
7779
),
@@ -378,4 +380,19 @@ void main() {
378380
expect(find.text(errorFormatText), findsOneWidget);
379381
});
380382
});
383+
384+
testWidgets('FocusNode can request focus', (WidgetTester tester) async {
385+
final FocusNode focusNode = FocusNode();
386+
await tester.pumpWidget(inputDatePickerField(
387+
focusNode: focusNode,
388+
));
389+
expect((tester.widget(find.byType(TextField)) as TextField).focusNode, focusNode);
390+
expect(focusNode.hasFocus, isFalse);
391+
focusNode.requestFocus();
392+
await tester.pumpAndSettle();
393+
expect(focusNode.hasFocus, isTrue);
394+
focusNode.unfocus();
395+
await tester.pumpAndSettle();
396+
expect(focusNode.hasFocus, isFalse);
397+
});
381398
}

0 commit comments

Comments
 (0)