TextField.canRequestFocus Deprecated
Summary
TextField.canRequestFocus
is deprecated. The same functionality can be
achieved by setting the canRequestFocus
parameter of the TextField
’s
FocusNode
.
Background
TextField.canRequestFocus
was added to support DropdownMenu
, which
has a TextField
that sometimes isn’t interactive. However, the same
functionality can be achieved by setting the canRequestFocus
parameter of a
TextField
’s FocusNode
. DropdownMenu
has been migrated to this approach,
and other use cases should follow the same pattern.
Apps that use TextField.canRequestFocus
display the following error when run
in debug mode: “Use focusNode
instead.”. Specifically, this means that users
should pass a FocusNode
to TextField.focusNode
with the
FocusNode.canRequestFocus
parameter set.
Migration guide
To migrate, remove the TextField.canRequestFocus
parameter. Create a
FocusNode
with the FocusNode.canRequestFocus
parameter set to the desired
value, and pass that to TextField.focusNode
.
Code before migration:
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
TextField(
canRequestFocus: false,
);
}
}
Code after migration:
class _MyWidgetState extends State<MyWidget> {
final FocusNode _focusNode = FocusNode(canRequestFocus: false);
@override
Widget build(BuildContext context) {
return TextField(
focusNode: _focusNode,
);
}
}
Timeline
Landed in version: 3.13.0-15.0.pre
In stable release: not yet (not in 3.13)
References
API documentation:
Relevant issues:
Relevant PRs: