简体   繁体   English

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

[英]Update Text() based on user input in a TextFormField() within a container in a ListVIew

I'm new to flutter and have a ... challenge with the above.我是扑扑的新手,并且对上述内容有一个......挑战。

I retrive data about several objects / documents from FireStore and create a ListView.我从 FireStore 检索有关多个对象/文档的数据并创建一个 ListView。

The logic, or lack there of, look like this:逻辑或缺乏逻辑如下所示:

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

This ListView contains several Containers where each Container display data from one object.此 ListView 包含多个 Container,其中每个 Container 显示来自一个对象的数据。 Within each Container there is a TextFormField that takes a double.在每个 Container 中都有一个 TextFormField 需要一个双精度值。 Based on this value, I want to change a Text() within the same container - a calculation.基于这个值,我想在同一个容器中更改一个 Text() - 一个计算。

Is this possible?这可能吗? If so - how?如果是这样 - 如何? I understand I can use TextEditController somehow, but then the result of tha calculation will be editable.我知道我可以以某种方式使用 TextEditController,但是计算的结果将是可编辑的。 Or?或者?

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

All you should need to do is use a setState to tell Flutter that the variable has changed and you want whichever Widgets that use it rebuilt:您需要做的就是使用setState告诉 Flutter 变量已更改,并且您希望重建使用它的任何 Widget:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM