简体   繁体   English

如何在 Flutter 中设置对特定输入文本字段的关注

[英]How set focus on specific input text field in Flutter

I have some TextFormField field in my flutter app.我的颤振应用程序中有一些TextFormField字段。 Here are the controllers of those fields.这是这些字段的控制器。

TextEditingController nameCtrl = TextEditingController();
TextEditingController idCtrl = TextEditingController();
TextEditingController nodeCtrl = TextEditingController();
TextEditingController unitCodeCtrl = TextEditingController();

I want to set focus to the unitCodeCtrl text field programmatically when an event fired (on button click) but can not figure out how can I do that.我想在触发事件(单击按钮时)时以编程方式将焦点设置到unitCodeCtrl文本字段,但不知道如何做到这一点。

...,
onPressed:(){
  //what can i do for set focus to unitcode text field?
}

First declare a focus node like this首先像这样声明一个焦点节点

final FocusNode unitCodeCtrlFocusNode = FocusNode();

then assign this focus node to that textfield然后将此焦点节点分配给该文本字段

TextFormField(
     controller: unitCodeCtrl,
     focusNode: unitCodeCtrlFocusNode,
)

And on the button click call below method, this will set a focus在按钮单击调用下面的方法时,这将设置一个焦点

onPressed:(){
  FocusScope.of(context).requestFocus(unitCodeCtrlFocusNode);
}

or或者

onPressed:(){
  unitCodeCtrlFocusNode.requestFocus();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM