在无状态小部件中使用 TextFormField 在颤振中非常困难 [英] Using TextFormField in Stateless widget is very difficult in flutter

查看:22
本文介绍了在无状态小部件中使用 TextFormField 在颤振中非常困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在无状态小部件中使用 TextFormField 以及 ScopedModel 来处理其中的文本并面临以下各种问题.

I am trying to use TextFormField in a stateless widget along with ScopedModel to deal with text in it and facing various issues as follow.

  1. 我尝试使用控制器作为字段,但每次我输入一些文本并在键盘上按完成时,文本都会被清除.不知道为什么.

  1. I tried using controller for field, but everytime I enter some text and press done on keyboard, text gets cleared. No idea as to why.

如果我删除控制器,文本会保留在字段中,但会产生关于如何从字段中获取文本的新问题.我通过使用回调 onFieldSubmitted 解决了它.

If I remove controller, text stays in field but new problem gets created as to how to get text from field. I solved it by using callback onFieldSubmitted.

但事实证明,onFieldSubmitted 只有在我们点击键盘上的完成按钮时才会被调用.如果我在字段中输入文本而不是单击确定,而是单击另一个字段,则不会调用回调,并且我将无法跟踪用户在字段中输入的内容.

But turns out, onFieldSubmitted is only getting called when we click on done button on keyboard. If I enter text in field and instead of clicking ok, click on another field, callback won't get called and I will have no way of tracking what user has entered in field.

有什么解决办法吗?

附上问题示例代码.

  class LoginPageStateless extends StatelessWidget {

  final loginUsernameController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: true,
      body: ScopedModelDescendant<AccountModel>(
        builder: (context, child, model) {
          return Form(
            //key: _formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                TextFormField(
                  style: TextStyle(fontSize: 15.0),
                  decoration: InputDecoration(
                    labelText: 'Email id',
                    hintText: 'johndoe@ipropal.com',
                  ),
                  controller: loginUsernameController,
                  onFieldSubmitted: model.updateLoginUsernameText,
                ),
                TextFormField(
                  style: TextStyle(fontSize: 15.0),
                  decoration: InputDecoration(
                    labelText: 'Password',
                  ),
                  controller: loginUsernameController,
                  onFieldSubmitted: model.updateLoginUsernameText,
                  obscureText: true,
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}

推荐答案

您不能也不应该使用 Stateless 小部件来存储长期变量.

You cannot and should not use Stateless widget for storing long-term variable.

问题是,这正是你想要的.TextEditingController 是一个应该在渲染之间保留的类实例.但是通过将其存储到 StatelessWidget 中,您基本上可以在每次更新后重新创建它.

The problem is, it's exactly what you are trying to. TextEditingController is a class instance that should be kept between renders. But by storing it into a StatelessWidget you basically recreate it after every update.

您应该将您的小部件转换为 Stateful.并将该控制器移动到 State 部分

You should instead convert your widget to Stateful. And move that controller into the State part

这篇关于在无状态小部件中使用 TextFormField 在颤振中非常困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆