我在一个有状态的小部件中有多个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),
],
),
答案 0 :(得分:0)
您应该在TextField中添加一个TextEditingController
TextField(
key: UniqueKey(),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 34, letterSpacing: 8),
controller: TextEditingController(),
),
答案 1 :(得分:0)
您需要创建全局变量fixedTriangles
并将其设置为TextEditingController()
您可以从该对象获取数据:
TextFormField()
var username = usernameController.text