基于ListVIew的容器内TextFormField()中用户输入的更新Text()

问题描述 投票:0回答:1

我是新手,要面对上述挑战。

我从FireStore检索有关多个对象/文档的数据,并创建ListView。

逻辑或缺乏逻辑,如下所示:

Container
    StreamBuilder
         ListView
             Container
                 Rows, Columns, Text and whatever to display each document from StreamBuilder.

此ListView包含多个容器,其中每个容器显示来自一个对象的数据。每个容器中都有一个TextFormField,它需要一个双精度值。基于此值,我想更改同一容器内的Text()-计算。

这可能吗?如果是这样-如何?我知道我可以以某种方式使用TextEditController,但是tha计算的结果将是可编辑的。还是?

return new ListView(//
  children: getMedicationItem(asyncSnapshot
    shrinkWrap: true,
    children: asyncSnapshot.data.documents.map((DocumentSnapshot document) {
      MyObject object = new MyObject(document, integerValue);
      backgroundColor = !backgroundColor;
      return new Container(
        color: backgroundColor? Colors.black26: Colors.white,
          child: new Row(
            children: <Widget>[
              new Flexible(
                child: TextFormField(
                  onChanged: (text) {
                    double calculation = 0.0;
                    if (text.length > 0)
                      calculation = double.tryParse(text) * 23;
                    **UPDATE "$calculation" in Text() below** 
                  },
                  keyboardType: TextInputType.number,
                  inputFormatters: [_decimal_validator],
                )
              ),
              new Flexible(
                child: Text("$calculation"),
              )
            ]
          ),
        );
      }
    ).toList(),
  )
);
flutter flutter-layout flutter-listview flutter-text
1个回答
0
投票

您需要做的就是使用setState告诉Flutter该变量已更改,并且您希望重建使用该变量的所有小部件:

if (text.length > 0){
  setState((){
    calculation = double.tryParse(text) * 23;
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.