Flutter:键盘自动显示

卡雷尔债务

我正在导航到我的 Flutter 应用程序中的下一页。

在下一页上有几个表单,用户可以在其中输入文本。

final _form2Key = GlobalKey<FormState>();
Padding(
  padding: EdgeInsets.only(top: 8.0),
  child: Container(
    width: screenWidth / 1.1,
    height: screenHeight / 5.5,
    child: Form(
      key: _form2Key,
      autovalidate: true,
      child: TextFormField(
        autofocus: true,
        validator: (val) {
          if (val.trim().length < 3 || val.isEmpty) {
            return "Too short";
          } else if (val.trim().length > 200) {
            return "Too long";
          } else {
            return null;
          }
        },
      onSaved: (val) => description = val,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        filled: true,
        fillColor: Colors.white,
        labelText: "",
        labelStyle: TextStyle(fontSize: 15.0),
        hintText: " ",
      ),
    ),
  ),
),

一切正常,但导航到这个新页面后,键盘会自动显示并聚焦在文本字段上。

是否可以避免键盘弹出并仅在用户按下表单时显示键盘?

提前致谢!

德洛哈尼

您的键盘会自动弹出,因为您已设置autofocustrue,请修改false或删除该属性以避免键盘自动弹出

final _form2Key = GlobalKey<FormState>();
Padding(
  padding: EdgeInsets.only(top: 8.0),
  child: Container(
    width: screenWidth / 1.1,
    height: screenHeight / 5.5,
    child: Form(
      key: _form2Key,
      autovalidate: true,
      child: TextFormField(
        autofocus: false, // modified
        validator: (val) {
          if (val.trim().length < 3 || val.isEmpty) {
            return "Too short";
          } else if (val.trim().length > 200) {
            return "Too long";
          } else {
            return null;
          }
        },
        onSaved: (val) => description = val,
        decoration: InputDecoration(
          border: OutlineInputBorder(),
          filled: true,
          fillColor: Colors.white,
          labelText: "",
          labelStyle: TextStyle(fontSize: 15.0),
          hintText: " ",
        ),
      ),
    ),
  ),
),

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章