在Flutter中将焦点从一个文本字段更改为下一个文本字段 [英] Changing focus from one text field to the next in Flutter

查看:106
本文介绍了在Flutter中将焦点从一个文本字段更改为下一个文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 textFormField 小部件。用户完成第一个文本字段后,我将重点关注下一个 textField 。在Flutter中有没有办法做到这一点?当前,完成按钮仅关闭键盘。我猜想 focusNode 类可能是解决这个问题的方法,但我不确定是否有人会提供很好的focusNode类实例吗?

I have two textFormField widgets. Once the user has completed the first text field I would like to focus on the next textField. Is there a way to do this in Flutter? Currently, the done button just closes the keyboard. I was guessing the focusNode class might be the answer to this but not really sure how that works does anyone have any good examples of focusNode class? Thanks in advance.

推荐答案

是的,焦点节点onFieldSubmitted flutter / material / TextFormField-class.html rel = noreferrer> TextFormField 可能是解决之道。

Yes, FocusNode and the onFieldSubmitted from a TextFormField are probably the way to go.


FocusScope.of(context).requestFocus(focusNode);

FocusScope.of(context).requestFocus(focusNode);

以下示例可能会有所帮助:

Here is an example that may help:

    FocusNode textSecondFocusNode = new FocusNode();

    TextFormField textFirst = new TextFormField(
      onFieldSubmitted: (String value) {
        FocusScope.of(context).requestFocus(textSecondFocusNode);
      },
    );

    TextFormField textSecond = new TextFormField(
      focusNode: textSecondFocusNode,
    );

    // render textFirst and textSecond where you want

您可能还想要从按钮而不是onFieldSubmitted触发 FocusScope.of(),但是希望上面的示例为您提供足够的上下文,以便为您的用例构建适当的解决方案。

You may also want to trigger FocusScope.of() from a button rather than onFieldSubmitted, but hopefully the above example gives you enough context to construct an appropriate solution for your use case.

这篇关于在Flutter中将焦点从一个文本字段更改为下一个文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆