Using TextFormField in Stateless widget is very difficult in flutter(在无状态小部件中使用 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.
我尝试使用控制器作为字段,但每次我输入一些文本并在键盘上按完成时,文本都会被清除.不知道为什么.
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 在颤振中非常困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在无状态小部件中使用 TextFormField 在颤振中非常困难
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01