繁体   English   中英

Flutter TextFormField controller 转换成文本

[英]Flutter TextFormField controller into Text

我有一个问题:(我有一个带有文本小部件的详细信息表单,我正在从我的数据库中获取值。我有一个带有 controller 的 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 中设置状态,但是 controller 也没有更新。

我试图声明一个新的双新利润,但提交后它仍然给我 null 回来....

希望可以有人帮帮我:)

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

它没有更新,因为当您更改 controller 的文本值时,小部件已经构建,如果您在有状态小部件中创建表单,您可以在 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 = '';

如果您没有在有状态小部件中构建它,您可以查看提供程序 package以更新没有 state 的值

暂无
暂无

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

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