【问题标题】:How do you take an input using a text field, put it into an equation and then display the output as text after a button is pressed in flutter?您如何使用文本字段进行输入,将其放入等式中,然后在按下按钮后将输出显示为文本?
【发布时间】:2019-12-23 20:16:58
【问题描述】:

我想在两个单独的文本字段中获取用户的输入,然后将这两个输入插入我自己的方程式中。当我使用print() 函数时,我现在拥有的代码似乎可以工作(它打印正确的值),但是当我将它放入Text() 元素时它不会更新相同的值。

这是我现阶段的代码:

class PageThree extends MaterialPageRoute<Null> {
  PageThree()
      : super(builder: (BuildContext ctx) {
          int age = 0;
          int annualspend = 0;
          int moneyneeded = 0;
          int temp = 0;
          String errormsg = '';

          return Scaffold(
            appBar: AppBar(
              title: Text("Retirement Calculator"),
              backgroundColor: Theme.of(ctx).accentColor,
              elevation: 2.0,
            ),
            body: Center(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.all(10),
                  child: TextField(
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: 'How much money do you spend every year?',
                        hintStyle: TextStyle(fontSize: 16),
                        errorText: 'Enter an approximate estimation.',
                        errorStyle:
                            TextStyle(color: Colors.blueAccent, fontSize: 13)),
                    onChanged: (String str) {
                      setState(() {
                        annualspend = int.parse(str);
                      });
                    },
                  ),
                ),
                Container(
                  margin: EdgeInsets.all(10),
                  child: TextField(
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: 'By what age do you wish to retire?',
                        hintStyle: TextStyle(fontSize: 16),
                        errorText: 'Enter an integer.',
                        errorStyle:
                            TextStyle(color: Colors.blueAccent, fontSize: 13)),
                    onChanged: (String str) {
                      setState(() {
                        age = int.parse(str);
                      });
                    },
                  ),
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    Container(
                        margin: const EdgeInsets.all(3.0),
                        child: Column(
                          children: <Widget>[
                            ButtonTheme(
                              minWidth: 395.0,
                              height: 20,
                              child: RaisedButton(
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(9),
                                ),
                                padding: EdgeInsets.all(12),
                                color: Colors.lightBlueAccent,
                                child: Text('Calculate',
                                    style: TextStyle(color: Colors.white)),
                                onPressed: () {
                                  setState(() {
                                    if (age <= 85) {
                                      moneyneeded = ((85 - age) * annualspend);
                                      errormsg = '';
                                    } else {
                                      moneyneeded = 0;
                                      errormsg =
                                          'You entered an age greater than 85. Please enter a resonable number.';
                                    }
                                  });
                                },
                              ),
                            ),
                            Text("$temp"),
                            Text(errormsg)
                          ],
                        )),
                  ],
                ),
              ],
            )),
          );
        });
}

我应该提一下,所有出现的setState 都显示错误,但它们似乎工作得很好。

在过去的两天里,我一直试图让它工作,但没有任何成功。我对颤动很陌生,所以我真的不知道该怎么做。非常感谢您的帮助。

【问题讨论】:

    标签: flutter input textfield


    【解决方案1】:

    问题在于将 temp 变量解析为 Text 小部件而不是 moneyyneeded 变量。只需将 Text(temp) 更改为 Text(moneyneeded) 即可修复它。并且您应该将此行添加到您的 TextField 因为输入是数字 keyboardType: TextInputType.number

    【讨论】:

    • 这是我在输入这个问题时犯的一个错误;我正在尝试使用临时变量,看看是否会改变任何东西,却忘了把它改回来。我添加了 keyboardType: TextInputType.number 并将文本小部件改回 Text('$moneyneeded') ,但问题仍然相同。当我点击“计算按钮”时,屏幕上没有任何变化。
    • 当我点击计算按钮时,我想将按钮下的数字更改为新的所需资金值。目前,即使在我更新值并单击按钮后,按钮下的数字仍保持为零。 This is what it looks like on my emulator
    • 你介意给我一张照片吗?也许问题出在我的模拟器上?
    • 哦,那是我的模拟器...谢谢!
    • 很抱歉再次打扰您,但我现在正在手机上试用该应用程序,但仍然无法使用。为了使其正常工作,您对原始代码进行了哪些更改?
    猜你喜欢
    • 2016-03-24
    • 2015-07-08
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多