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



我是新来的,有一个。。。挑战以上内容。

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

逻辑,或者说缺乏,看起来是这样的:

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

此ListView包含多个Container,其中每个Container显示来自一个对象的数据。在每个Container中都有一个使用double的TextFormField。基于这个值,我想在同一个容器中更改一个Text((——一个计算。

这可能吗?如果是,怎么做?我知道我可以以某种方式使用TextEditController,但计算结果将是可编辑的。或

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(),
)
);

您所需要做的就是使用setState来告诉Flutter变量已经更改,并且您希望重建使用它的Widget:

if (text.length > 0){
setState((){
calculation = double.tryParse(text) * 23;
});
}

最新更新