Deprecated TextField.canRequestFocus
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) {
return 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: Reverted, waiting to reland
In stable release: Not yet
References
#API documentation:
Relevant issues:
Relevant PRs:
- Add requestFocusOnTap to DropdownMenu
- Replace TextField.canRequestFocus with TextField.focusNode.canRequestFocus
除非另有说明,本文档之所提及适用于 Flutter 的最新稳定版本,本页面最后更新时间: 2024-10-09。 查看文档源码 或者 为本页面内容提出建议。