【发布时间】: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 都显示错误,但它们似乎工作得很好。
在过去的两天里,我一直试图让它工作,但没有任何成功。我对颤动很陌生,所以我真的不知道该怎么做。非常感谢您的帮助。
【问题讨论】: