如何在Flutter中完成最大字符后将焦点切换到其他文本字段?

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

How can i change the focus to other textfield after i complete max characters in Flutter?

问题

如何在Flutter中在达到最大字符数后而不需要按下下一个/完成按钮的情况下将焦点更改到其他节点?如何使用Focus节点将控制权传递给其他TextField?我想在每个TextField的前4个字符后将控制权传递给下一个TextField。

Widget serailKey1() {
    return TextField(
        controller: controller_key1,
        focusNode: _serialKeyFN1,
        textAlign: TextAlign.center,
        keyboardType: TextInputType.text,
        textCapitalization: TextCapitalization.characters,
        style: textStyle,
        maxLength: 4,

        decoration: new InputDecoration(
            counterText: "",
            border: new OutlineInputBorder(
                borderRadius: const BorderRadius.all(
                    const Radius.circular(0.0),
                ),
            ),
        ),
        onEditingComplete: () => FocusScope.of(context).requestFocus(_serialKeyFN2),
    );
}

Widget serailKey2() {
    return TextField(
        controller: controller_key2,
        focusNode: _serialKeyFN2,
        textAlign: TextAlign.center,
        keyboardType: TextInputType.text,
        style: textStyle,
        textCapitalization: TextCapitalization.characters,
        maxLength: 4,
        decoration: new InputDecoration(
            counterText: "",
            border: new OutlineInputBorder(
                borderRadius: const BorderRadius.all(
                    const Radius.circular(0.0),
                ),
            ),
        ),
        onEditingComplete: () => FocusScope.of(context).requestFocus(_serialKeyFN3),
    );
}

这些是您提供的两个TextField小部件,它们具有用于控制焦点的FocusNode,并在编辑完成时将焦点传递给下一个TextField。

英文:

How do I change the focus to other node after completion max characters without pressing next/done in Flutter ?. How do I pass the control to other TextField using Focus node. I want to pass the control to next textfield after 4 characters of every textfield.

Widget serailKey1() {
        return TextField(
            controller: controller_key1,
            focusNode: _serialKeyFN1,
            textAlign: TextAlign.center,
            keyboardType: TextInputType.text,
            textCapitalization: TextCapitalization.characters,
            style: textStyle,
            maxLength: 4,
    
            decoration: new InputDecoration(
    
              counterText: "",
              border: new OutlineInputBorder(
                borderRadius: const BorderRadius.all(
                  const Radius.circular(0.0),
                ),
              ),
          ),
          onEditingComplete: ()=> FocusScope.of(context).requestFocus(_serialKeyFN2),
    
        );
      }
    
      Widget serailKey2() {
        return TextField(
            controller: controller_key2,
            focusNode: _serialKeyFN2,
            textAlign: TextAlign.center,
            keyboardType: TextInputType.text,
            style: textStyle,
            textCapitalization: TextCapitalization.characters,
            maxLength: 4,
            decoration: new InputDecoration(
              counterText: "",
              border: new OutlineInputBorder(
                borderRadius: const BorderRadius.all(
                  const Radius.circular(0.0),
                ),
              ),
    
            ),
          onEditingComplete: ()=> FocusScope.of(context).requestFocus(_serialKeyFN3),
    
        );
      } 

答案1

得分: 0

这对我来说很好用:

    FocusNode fNode = FocusNode();
    ...
    TextField(onChanged: (value){ 
      if(value.length == 4) 
        FocusScope.of(context).requestFocus(fNode);
    }),
    TextField(focusNode: fNode),
英文:

This works fine for me:

    FocusNode fNode = FocusNode();
    ...
    TextField(onChanged: (value){ 
      if(value.length == 4) 
        FocusScope.of(context).requestFocus(fNode);
    }),
    TextField(focusNode: fNode),

huangapple
  • 本文由 发表于 2020年1月3日 14:31:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574130.html
匿名

发表评论

匿名网友

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

确定