Changing focus from one text field to the next in Flutter

前端 未结 4 1749
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 03:40

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

4条回答
  •  情歌与酒
    2021-02-05 04:02

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

    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
    

    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.

提交回复
热议问题