【问题标题】:Why my page is reloading when my TextFormField is focused?为什么当我的 TextFormField 获得焦点时我的页面会重新加载?
【发布时间】:2021-09-25 08:43:08
【问题描述】:

当我触摸 TextField 时,它会自动重新加载我的页面,我不能放一个字母

我尝试了很多方法,例如删除 setState 并且它有效,但我需要它来将我的值保存在数据中,我发现该方法可以将我的未来置于 initstate 但我不知道该怎么做,我'不确定这是解决方案。 我在 StateFulWidget 我也使用了 globalKey,但它在 build 方法之外。

这是我的代码:

class _UpdateProfileState extends State<UpdateProfile> {

 final GlobalKey<FormState> _formKey  = GlobalKey<FormState>();

...
//My future
 body: Form(
        key: _formKey,
       child: Column(
          children: <Widget>[
            FutureBuilder<DocumentSnapshot>(
              future: Users
                  .doc(uid)
                  .get(),
              builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text("Something went wrong");
                }
                if (snapshot.hasData && !snapshot.data!.exists) {
                  return Text("Document does not exist");
                }
                if (snapshot.connectionState == ConnectionState.done) {
                  return displayUserInformation(context, snapshot);
                } else {
                  return SizedBox(height: 400,
                      child: Center(child: CircularProgressIndicator()));
                }
              },
            ),
          ],
        ),
     ),
    );
  }

...
 //And this is My textFormField
 Padding(
           padding: const EdgeInsets.all(16.0),
           child: TextFormField(
             onChanged: (value) =>
                 setState(() => _name = value.trim()),
             initialValue: "${data['displayName'].toString()}",
               cursorColor: DarkTurquoise,
             decoration: InputDecoration(
               labelText: 'Name',
               labelStyle: TextStyle(
                 color: SeaTurquoise
               ),
               focusedBorder: UnderlineInputBorder(
                 borderSide:  BorderSide(color: SeaTurquoise, width: 2.0),),
               enabledBorder: UnderlineInputBorder(
                 borderSide: BorderSide(color: DarkGrey),
               ),
             ),
           ),
         ),

感谢您的回答

【问题讨论】:

  • 尝试将“onChanged”替换为“onEditingComplete”
  • 如果您在未来的构建器中使用文本字段,它将重新构建,您将无法使用它
  • 当我用 onEditing Complete 替换 onChanged 时,它带有红色下划线,我在 Internet 上找不到 onEditingComplete 的方法
  • 是的,我忘记了我在未来,但我必须找到一个解决方案来显示我的数据并对其进行编辑
  • 你能展示完整的课程吗?

标签: firebase flutter dart future textformfield


【解决方案1】:

将下面的代码放在您用来提交表单的按钮上,它将从您的文本字段中获取焦点并将其提供给您可以使用的按钮,您也可以使用焦点节点转到此link 以了解更多信息

       FocusScope.of(context).unfocus();
       FocusScope.of(context).requestFocus(new FocusNode());

【讨论】:

  • 我把它放在onpressed () { FocusScope.of(context).unfocus(); FocusScope.of(context).requestFocus(new FocusNode());},但它仍然不起作用,我的页面每次我写信时都会重新加载
  • 我需要查看您的完整代码,我认为您正在返回带有未来构建器的文本字段
  • 也使用文本编辑控制器来获取和设置数据
  • 所以我必须删除我的 setState 吗?
  • 我将 setState 更改为 textEditingController 并且它可以工作,谢谢
猜你喜欢
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 1970-01-01
  • 1970-01-01
  • 2019-12-21
  • 2020-02-02
相关资源
最近更新 更多