Flutter:如何将焦点转移到下一个 TextFormField?

英德雷克

我是 Flutter 的新手,希望在当前 TextFormField 达到 maxLength 时从一个 TextFormField 移动到另一个,而无需按 Enter。这意味着我可以在小时 TextFormField 上输入 2 位数字,并且聚焦的 TextFormField 将移动到分钟 TextFormField 等等。

我曾尝试使用 nextFocus(),但是这仅在按 Enter 时有效。

小时输入的示例小部件:

Widget _buildHH(){
    return Theme(
      data: Theme.of(context).copyWith(primaryColor: Colors.orange),
      child: Expanded(
        child:
        TextFormField(
          maxLength: 2,
          cursorColor: Colors.orange,
          style: TextStyle(color: Colors.orange),
          decoration: InputDecoration(
              border: OutlineInputBorder(),
              labelText: 'HH'
          ),
          onEditingComplete: () => FocusScope.of(context).nextFocus(),
          keyboardType: TextInputType.number,
          validator: (String value){
            return null;
          },
          onSaved: (String value){
            _HH = value;
          },
        ),
      )
    );
  }


杰斯温
final node = FocusScope.of(context);

在 build 方法下初始化这个变量

onChanged: (value) {
                   if (value.length == 2) node.nextFocus();
                     },

将此 onChanged 方法添加到您的 TextFormField 中,如果文本字段中的文本长度等于 2,它将自动移动焦点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章