如何在Flutter中避免使用TextFormField中的focusNode?

huangapple go评论72阅读模式
英文:

How to avoid focusnode use in textformfield in flutter?

问题

我已经使用focusNode来将光标焦点放在文本表单字段上。但我定义了一个全局的textformfield小部件,但只在单个屏幕上使用了focus node。现在的问题是,由于小部件中的通用focusnode,自动激活了所有字段的光标。我们如何设置仅在登录屏幕中focus node才能工作,否则将避免该条件。

FocusNode textFocusOnFirstField = FocusNode();

TextFormField(
  // 在所有的TextFormField中使用这个变量
  // enabled: isEnable, // 在所有的TextFormField中使用这个变量
  focusNode: textFocusOnFirstField,
  readOnly: readOnly,
)
英文:

I have used focusNode to focus cursor on text form field. But I defined global textformfield widget. but used focus node in single screen only. Now the problem is automatically all the field cursor gets activated. because of that common focusnode in widget. How we can set that only in slogin screen focus node will work or else that condition will avoid.

 FocusNode textFocusOnFirstField = FocusNode();

    TextFormField(
              // here use the variable in all the TextFormField
              // enabled: isEnable, // here use the variable in all the TextFormField
              focusNode: textFocusOnFirstField ,
              readOnly: readOnly,

答案1

得分: 1

使用以下代码在initState()中,以便在屏幕加载时取消所有文本字段的焦点:

FocusScope.of(context).unfocus();
英文:
FocusScope.of(context).unfocus();

Use this in initState() so that it unfocuses all the textfield as soon as screen loads.

huangapple
  • 本文由 发表于 2023年1月9日 17:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定