0

我有一个问题:(我有一个带有文本小部件的详细信息表单,我正在从我的数据库中获取值。我有一个带有控制器的 TextFormField,因为我需要设置实际价格,提交后,新值将在TextValueField,但我希望它显示在普通的文本小部件上。

我希望你能理解我的问题。但这里是我的代码

          TextField(
                      decoration: InputDecoration(labelText: 'actual cost'),
                      controller: _profitController,
                      keyboardType:
                          TextInputType.numberWithOptions(decimal: true),
                      onSubmitted: (val) {
                        double amount =
                            double.parse(selectedEntry.amount); // 1 value
                        double aktPrice =
                            double.parse(val); // 2 value from this textfield
                        double profit = aktPrice * amount; // calc

                        _endProfitController.text = profit.toString();

                        newProfit = profit;
                      },
                    ),
                    TextFormField(
                      readOnly: true,
                      decoration: InputDecoration(labelText: "profit"),
                      controller:
                          _endProfitController, //this is getting updated with the controller
                      textAlign: TextAlign.left,
                      style: TextStyle(
                        fontSize: 20,
                        color: Colors.grey[700],
                      ),
                    ),
                    Text(
                      'test profit: ' +
                          _endProfitController.text.toString() + newProfit.toString() +
                          ' €', // this is not getting updated, and newProfit says "Null"...
                      textAlign: TextAlign.left,
                      style: TextStyle(
                        fontSize: 20,
                        color: Colors.grey[700],
                      ),
                    ),

好吧,我怎样才能在我的文本小部件中获取值?我试图在 onSubmitted 中设置状态,但控制器也没有更新。

我试图声明一个新的双 newProfit 但提交后它仍然给我空返回....

希望可以有人帮帮我 :)

最好的问候并感谢您的时间:)

4

1 回答 1

0

它没有更新,因为当您更改控制器的文本值时,如果您在有状态的小部件中创建表单,您可以在 onChanged 内调用 setState() 以便字段的值更新您将使用的字符串显示文本,类似这样。

TextFormField(
                decoration: InputDecoration(labelText: "profit"),
                controller:
                    _endProfitController, //this is getting updated with the controller
                textAlign: TextAlign.left,
                onChanged: (value) {
                  setState(() {
                    valueText = _endProfitController.text;
                  });
                  print(valueText);
                },
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.grey[700],
                ),
              ),
              Text(
                'test profit: ' +
                    valueText +
                    ' €', // this is not getting updated, and newProfit says "Null"...
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.grey[700],
                ),
              ),

将获取控制器文本值的变量必须在构建方法之外

String valueText = '';

如果您没有在有状态小部件中构建它,您可以查看Provider 包以更新没有状态的值

于 2021-06-10T23:35:35.623 回答