在材质应用程序中颤动多个文本表单字段 - 重置文本



我在有状态小部件中有多个TextFormFields。当我向下滚动到底部,然后如果我滚动到顶部时,我在第一个 TextFormFields 中键入的文本将被重置。 当小部件不在当前视图中时,Flutter 似乎会处理小部件,所以我尝试使用 UniqueKey((,但仍然没有成功。我错过了什么?

body: ListView(
key: UniqueKey(),
padding: const EdgeInsets.all(8.0),
children: <Widget>[
TextField(
key: UniqueKey(),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 34, letterSpacing: 8),
),
SizedBox(height: 50),
TextField(
key: UniqueKey(),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 34, letterSpacing: 8),
),
SizedBox(height: 50),
TextField(
key: UniqueKey(),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 34, letterSpacing: 8),
),
SizedBox(height: 50),
TextField(
key: UniqueKey(),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 34, letterSpacing: 8),
),
SizedBox(height: 50),
TextField(
key: UniqueKey(),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 34, letterSpacing: 8),
),
SizedBox(height: 50),
TextField(
key: UniqueKey(),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 34, letterSpacing: 8),
),
SizedBox(height: 50),
TextField(
key: UniqueKey(),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 34, letterSpacing: 8),
),
SizedBox(height: 50),
TextField(
key: UniqueKey(),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 34, letterSpacing: 8),
),
SizedBox(height: 50),
],
),

您需要创建TextEditingController()的全局变量并将其设置为您的TextFormField()

您可以从该对象获取数据:

var username = usernameController.text

var usernameController = TextEditingController();
var passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
TextFormField(
controller: usernameController,
keyboardType: TextInputType.text,
style: TextStyle(fontSize: 34, letterSpacing: 8),
),
TextFormField(
controller: passwordController,
keyboardType: TextInputType.text,
style: TextStyle(fontSize: 34, letterSpacing: 8),
),
]);
}

您应该将 TextEditorController 添加到您的 TextField

TextField(
key: UniqueKey(),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 34, letterSpacing: 8),
controller: TextEditingController(),
),

相关内容

  • 没有找到相关文章

最新更新