Changing focus from one text field to the next in Flutter

前端 未结 4 1751
佛祖请我去吃肉
佛祖请我去吃肉 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:12

    This is how I did it:

      var _focusNodes = List.generate(6, (index) => FocusNode()));
    

    And in the TextFormField:

      TextFormField(
        focusNode: _focusNodes[i],
        maxLines: 1,
        textInputAction: TextInputAction.next,
        onChanged: (text) {
          if (i < _controllers.length) {
            if (text.isEmpty)
              _focusNodes[i - 1].requestFocus();
            else
              _focusNodes[i + 1].requestFocus();
          }
        },
      ),
    

提交回复
热议问题