在Material App中移动多个TextFormFields-重置文本

时间:2019-09-04 04:23:25

标签: flutter flutter-layout

我在一个有状态的小部件中有多个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),
    ],
  ),

2 个答案:

答案 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