android 在Flutter中按键盘清除键(x)时,如何向后更改焦点?

4xy9mtcn  于 2023-02-02  发布在  Android
关注(0)|答案(2)|浏览(113)

这是我的代码,我使用的是textformfield,这是用于otp输入的,所以每当用户点击清除按钮时,我想把注意力集中在前一个文本字段上。

TextFormField(
    controller: controller,
    autofocus: true,
    focusNode: focusNod,
    validator: (String value) {
      if (value.isEmpty) {
        return "";
      } else {
        setState(() {
          otp = one.text + two.text + three.text + 
          four.text;
          print("otp : " + otp.toString());
        });
        return null;
      }
    },
    onChanged: (String number) {
      if (number.isNotEmpty) {
        focus.requestFocus();
      }

      navigation();
    },
    keyboardType: TextInputType.number,
    inputFormatters: [
      FilteringTextInputFormatter.digitsOnly,
      LengthLimitingTextInputFormatter(1)
    ],
    style: TextStyle(color: Colors.white),
    decoration: InputDecoration(
      helperText: '',
      border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: BorderSide(color: Colors.green)),
      focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: BorderSide(color: Colors.white)),
      errorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: BorderSide(color: Colors.white)),
      focusedErrorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: BorderSide(color: Colors.white)),
      enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: BorderSide(color: Colors.white)),
    ),
    textInputAction: action,
    cursorColor: Color(0xFFFFFFFF),
  ),
w8f9ii69

w8f9ii691#

您应该尝试添加textInputAction:文本输入操作.上一个,文本表单字段中的

TextFormField(
textInputAction: TextInputAction.previous,
)
58wvjzkj

58wvjzkj2#

在OTP文本字段中,只需写入此方法即可返回导航。

TextformField(
onChanged: ((value) {
                      if (value.length == 1) {
                        FocusScope.of(context).nextFocus();
                      } else {
                        FocusScope.of(context).previousFocus();
                      }
                    })),

相关问题