Skip to content

Commit

Permalink
Add InputDatePickerFormField.focusNode prop (flutter#136673)
Browse files Browse the repository at this point in the history
Adds `focusNode` prop to `InputDatePickerFormField` widget.

Fixes flutter#105881
  • Loading branch information
piedcipher authored Oct 23, 2023
1 parent aea5621 commit 2de1af4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<InputDatePickerFormField> createState() => _InputDatePickerFormFieldState();
}
Expand Down Expand Up @@ -266,6 +270,7 @@ class _InputDatePickerFormFieldState extends State<InputDatePickerFormField> {
onFieldSubmitted: _handleSubmitted,
autofocus: widget.autofocus,
controller: _controller,
focusNode: widget.focusNode,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void main() {
ThemeData? theme,
Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates,
bool acceptEmptyDate = false,
FocusNode? focusNode,
}) {
return MaterialApp(
theme: theme ?? ThemeData.from(colorScheme: const ColorScheme.light()),
Expand All @@ -72,6 +73,7 @@ void main() {
fieldLabelText: fieldLabelText,
autofocus: autofocus,
acceptEmptyDate: acceptEmptyDate,
focusNode: focusNode,
),
),
),
Expand Down Expand Up @@ -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);
});
}

0 comments on commit 2de1af4

Please sign in to comment.