【问题标题】:Flutter: keyboard automatically shows upFlutter:键盘自动出现
【发布时间】:2020-04-28 21:20:57
【问题描述】:

我正在导航到我的 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: " ",
      ),
    ),
  ),
),

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

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

提前致谢!

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您的键盘自动弹出是因为您将autofocus设置为true,请修改为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: " ",
            ),
          ),
        ),
      ),
    ),
    

    【讨论】:

      猜你喜欢
      • 2012-03-08
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2019-01-05
      • 1970-01-01
      相关资源
      最近更新 更多