Dart 单击按钮时更新颤振中的文本字段
我正在开发一个关于颤振的应用程序,在主屏幕上,我有一个按钮,可以打开另一个屏幕,它在一个方法中。在那个屏幕上,我想做一些计算,比如获取用户输入和更新文本字段,单击按钮,调用方法calculateAmount更新变量total,该变量反映在文本字段上,但文本字段不更新,它只在按键盘上的finished(完成)时更新。。。如何才能完成这项任务。 这是我的代码:Dart 单击按钮时更新颤振中的文本字段,dart,flutter,Dart,Flutter,我正在开发一个关于颤振的应用程序,在主屏幕上,我有一个按钮,可以打开另一个屏幕,它在一个方法中。在那个屏幕上,我想做一些计算,比如获取用户输入和更新文本字段,单击按钮,调用方法calculateAmount更新变量total,该变量反映在文本字段上,但文本字段不更新,它只在按键盘上的finished(完成)时更新。。。如何才能完成这项任务。 这是我的代码: import 'package:flutter/material.dart'; void main() { r
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: new homePage()
));
}
class homePage extends StatefulWidget {
@override
homePageState createState() => new homePageState();
}
class homePageState extends State<homePage> {
double metal = 0.0;
double total = 0.0;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("APP-Title",), backgroundColor: Colors.orange),
body: new Container(
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Expanded(child: new RaisedButton.icon(
color: Colors.orange,
icon: const Icon(
Icons.info, size: 25.0, color: Colors.white,),
label: new Text('Calculate', style: new TextStyle(
color: Colors.white
)),
onPressed: () {
calculateWindow();
),),
],
)
],
)
),
),
);
}
void calculateWindow(){
Navigator.of(context).push(
new MaterialPageRoute(
builder: (context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Calculator'),
backgroundColor: Colors.orange,
),
body: new ListView(
children: <Widget>[
new Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Container(
height: 50.0,
width: 360.0,
decoration: new BoxDecoration(
color: Colors.orange,
shape: BoxShape.rectangle,
),
child: new Center(
child: new Row(
children: <Widget>[
new Expanded(
child: new Container(
child: new Text("Gold & Silver in Sold & Ornaments",
style: textStyle,
textAlign: textAlign
),
),
),
new Container(
height: 40.0,
width: 80.0,
decoration: new BoxDecoration(
color: Colors.white),
child: new TextField(
keyboardType: TextInputType.number,
onSubmitted : (String value) {
try {
metal = double.parse(value.toString());
print(total);
} catch (exception) {
metal = 0.0;
}
}
),
),
],
),
),
),
],
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Expanded(
child: new Container(
width: 50.0,
child: new RaisedButton.icon(
color: Colors.grey,
icon: const Icon(
Icons.check, size: 25.0, color: Colors.white,),
label: new Text('Calculate', style: new TextStyle(
color: Colors.white,
)),
onPressed: calculateAmount,
),
),
),
],
),
new Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Container(
height: 50.0,
width: 350.0,
decoration: new BoxDecoration(
color: Colors.blueGrey,
shape: BoxShape.rectangle,
),
child: new Center(
child: new Row(
children: <Widget>[
new Expanded(
child: new Container(
child: new Text("Total Amount:",
style: new TextStyle(
color: Colors.white,),
textAlign: TextAlign.left,),
),
),
new Container(
child: new Text(
'$total',
textAlign: TextAlign.left,
overflow: TextOverflow.ellipsis,
style: textStyle,
textDirection: TextDirection.ltr,
)
),
],
),
),
),
],
),
],
),
);
},
),
);
}
void calculateAmount(){
setState((){
total = metal + 0.025;
});
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(新材料)PP(
debugShowCheckedModeBanner:false,
主页:新主页()
));
}
类主页扩展了StatefulWidget{
@凌驾
homePageState createState()=>新建homePageState();
}
类homePageState扩展了状态{
双金属=0.0;
双倍合计=0.0;
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“应用程序标题”),背景颜色:Colors.orange),
主体:新容器(
孩子:新中心(
子:新列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新行(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新扩展(子项:new RaisedButton.icon(
颜色:颜色。橙色,
图标:常量图标(
Icons.info,大小:25.0,颜色:Colors.white,),
标签:新文本('计算'),样式:新文本样式(
颜色:颜色。白色
)),
已按下:(){
计算窗口();
),),
],
)
],
)
),
),
);
}
void calculateWindow(){
导航器.of(上下文).push(
新材料路线(
生成器:(上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“计算器”),
背景颜色:Colors.orange,
),
正文:新列表视图(
儿童:[
新行(
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
新容器(
身高:50.0,
宽度:360.0,
装饰:新盒子装饰(
颜色:颜色。橙色,
形状:BoxShape.rectangle,
),
孩子:新中心(
孩子:新的一排(
儿童:[
新扩展(
子容器:新容器(
儿童:新文本(“售出的金银首饰”,
风格:textStyle,
textAlign:textAlign
),
),
),
新容器(
身高:40.0,
宽度:80.0,
装饰:新盒子装饰(
颜色:颜色。白色),
孩子:新文本字段(
键盘类型:TextInputType.number,
OnSubmited:(字符串值){
试一试{
metal=double.parse(value.toString());
印刷品(总数);
}捕获(例外){
金属=0.0;
}
}
),
),
],
),
),
),
],
新行(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新扩展(
子容器:新容器(
宽度:50.0,
子级:新建RaisedButton.icon(
颜色:颜色。灰色,
图标:常量图标(
Icons.check,大小:25.0,颜色:Colors.white,),
标签:新文本('计算'),样式:新文本样式(
颜色:颜色,白色,
)),
onPressed:calculateAmount,
),
),
),
],
),
新行(
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
新容器(
身高:50.0,
宽度:350.0,
装饰:新盒子装饰(
颜色:颜色。蓝灰色,
形状:BoxShape.rectangle,
),
孩子:新中心(
孩子:新的一排(
儿童:[
新扩展(
子容器:新容器(
子项:新文本(“总额:”,
样式:新文本样式(
颜色:颜色。白色,),
textAlign:textAlign.left,),
class TextFieldEx extends StatefulWidget {
@override
_TextFieldExState createState() => new _TextFieldExState();
}
class _TextFieldExState extends State<TextFieldEx> {
TextEditingController _c ;
double _metal = 0.0;
double _total = 0.0;
String _text = "initial";
@override
void initState() {
_c = new TextEditingController();
super.initState();
}
@override
void dispose(){
_c?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new TextField(
keyboardType: TextInputType.number,
onChanged: (v)=>setState((){_text=v;}),
controller: _c,
),
new RaisedButton(
child: new Text("Update"),
onPressed: (){
setState((){
_metal = double.parse(_c.text);
_total = _metal+0.025;
_c.text = "";
});
},
),
new Text("Text Input: $_text"),
new Text("Metal :$_metal"),
new Text("Total:$_total")
],
)
)
);
}
}