Function 如何将函数的结果放入Flatter中的文本小部件中?
我是这门语言的新手,正在开发BMI(体重指数)应用程序。如下图所示: 我接受用户输入并计算结果,然后在控制台中打印结果。例如:Function 如何将函数的结果放入Flatter中的文本小部件中?,function,android-studio,flutter,dart,mobile-development,Function,Android Studio,Flutter,Dart,Mobile Development,我是这门语言的新手,正在开发BMI(体重指数)应用程序。如下图所示: 我接受用户输入并计算结果,然后在控制台中打印结果。例如: I/flutter ( 4500): 2.25 I/flutter ( 4500): 20.0 // this is the result of BMI I/flutter ( 4500): you are at your ideal weight. // this is the explanation 我想在一个文本小部件中显示这些结果,让用户看到它们。但我不
I/flutter ( 4500): 2.25
I/flutter ( 4500): 20.0 // this is the result of BMI
I/flutter ( 4500): you are at your ideal weight. // this is the explanation
我想在一个文本小部件中显示这些结果,让用户看到它们。但我不知道怎么做。如何从函数中获取结果值并将其添加到接口
这是我的代码,在代码中我指出了我在哪里卡住了。主要功能:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'calculation.dart';
void main() => runApp(MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'BMI';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title),
centerTitle: true,
backgroundColor: Colors.amber[900],
),
body: Center(
child: MyStatefulWidget(),
),
),
);
}
}
enum SingingCharacter { lafayette, jefferson }
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
SingingCharacter _character = SingingCharacter.lafayette;
double height=1;
double weight=1;
String info1="";
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10.0),
child:Scrollbar(
child:SingleChildScrollView(
child:Card(
color: Colors.amber[50],
child:Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(0, 30, 10, 10),
child: Text("Sex:",
style:TextStyle(fontSize: 24, letterSpacing: 1.0)),
),
ListTile(
title: const Text('Female',
style:TextStyle(fontSize: 18, letterSpacing: 1.0)
),
leading: Radio(
activeColor: Colors.orange,
value: SingingCharacter.lafayette,
groupValue: _character,
onChanged: (SingingCharacter value) {
setState(() {
_character = value;
});
},
),
),
ListTile(
title: const Text('Male',
style:TextStyle(fontSize: 18, letterSpacing: 1.0,)
),
leading: Radio(
activeColor: Colors.orange,
value: SingingCharacter.jefferson,
groupValue: _character,
onChanged: (SingingCharacter value) {
setState(() {
_character = value;
});
},
),
),
SizedBox(height: 10.0),
Text("Your height:",
style:TextStyle(fontSize: 24, letterSpacing: 1.0)
),
SizedBox(height: 10),
Padding(
padding: const EdgeInsets.fromLTRB(30, 0, 50, 10),
child: TextField(
decoration: new InputDecoration(labelText: "Your height(cm)"),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be entered
onSubmitted: (input1){
if(double.parse(input1)>0){
setState(() => height=double.parse(input1));
print(input1);
}
},
),
),
SizedBox(height: 20),
Text("Your weight:",
style:TextStyle(fontSize: 24, letterSpacing: 1.0)
),
SizedBox(height: 10),
Padding(
padding: const EdgeInsets.fromLTRB(30, 0, 50, 10),
child: new TextField(
decoration: new InputDecoration(labelText: "Your weight(kg)"),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be entered
onSubmitted: (input2){
if (double.parse(input2)>0){
// print(weight);
setState(() {
return weight=double.parse(input2);
});
}
},
),),
SizedBox(height: 10,),
RaisedButton(
padding:EdgeInsets.fromLTRB(20, 5, 20, 5),
onPressed: () async{
await Calculation(height, weight);
// return Calculation.info1 ??? //i don't know how to take info1 from calculation function
},
color: Colors.amber[900],
child:Text(
'Calculate',
style:TextStyle(
color: Colors.white,
fontSize: 30,
letterSpacing: 2.0,
),
),
),
SizedBox(height: 20,),
Text('Results: $height,$weight'),
// Text('Calculation.info1'), // i do not know how to show info in a text box.
],
),
),
),
),
);
}
}
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
导入“calculation.dart”;
void main()=>runApp(MyApp());
///这是主要的应用程序小部件。
类MyApp扩展了无状态小部件{
静态常量字符串_title='BMI';
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:_标题,
家:脚手架(
appBar:appBar(标题:常量文本),
标题:对,
背景颜色:颜色。琥珀色[900],
),
正文:中(
子项:MyStatefulWidget(),
),
),
);
}
}
enum SingingCharacter{lafayette,jefferson}
///这是主应用程序实例化的有状态小部件。
类MyStatefulWidget扩展了StatefulWidget{
MyStatefulWidget({Key}):超级(Key:Key);
@凌驾
_MyStatefulWidgetState createState()=>\u MyStatefulWidgetState();
}
///这是MyStatefulWidget附带的私有状态类。
类_MyStatefulWidgetState扩展状态{
SingingCharacter _character=SingingCharacter.lafayette;
双倍高度=1;
双倍重量=1;
字符串info1=“”;
小部件构建(构建上下文){
返回填充(
填充:常数边集全部(10.0),
子:滚动条(
子:SingleChildScrollView(
孩子:卡片(
颜色:颜色。琥珀色[50],
子:列(
儿童:[
填充物(
填充:从LTRB(0,30,10,10)开始的常数边集,
儿童:文本(“性别:”,
样式:TextStyle(字体大小:24,字母间距:1.0)),
),
列表砖(
标题:const Text(‘女性’,
样式:文本样式(字体大小:18,字母间距:1.0)
),
领先:无线电(
activeColor:Colors.orange,
值:SingingCharacter.lafayette,
groupValue:_字符,
onChanged:(SingingCharacter值){
设置状态(){
_字符=值;
});
},
),
),
列表砖(
标题:常量文本(“男性”,
样式:TextStyle(字体大小:18,字母间距:1.0,)
),
领先:无线电(
activeColor:Colors.orange,
值:SingingCharacter.jefferson,
groupValue:_字符,
onChanged:(SingingCharacter值){
设置状态(){
_字符=值;
});
},
),
),
尺寸箱(高度:10.0),
文本(“您的身高:”,
样式:TextStyle(字体大小:24,字母间距:1.0)
),
尺寸箱(高度:10),
填充物(
填充:LTRB(30,0,50,10)中的常数边集,
孩子:TextField(
装饰:新输入装饰(标签文字:“您的身高(厘米)”),
键盘类型:TextInputType.number,
输入格式化程序:[
FilteringPutFormatter.digitsOnly
],//只能输入数字
提交:(输入1){
if(double.parse(input1)>0){
setState(()=>height=double.parse(input1));
打印(输入1);
}
},
),
),
尺寸箱(高度:20),
文本(“您的体重:”,
样式:TextStyle(字体大小:24,字母间距:1.0)
),
尺寸箱(高度:10),
填充物(
填充:LTRB(30,0,50,10)中的常数边集,
孩子:新文本字段(
装饰:新输入装饰(标签文字:“您的体重(kg)”,
键盘类型:TextInputType.number,
输入格式化程序:[
FilteringPutFormatter.digitsOnly
],//只能输入数字
提交:(输入2){
if(double.parse(input2)>0){
//打印(重量);
设置状态(){
返回权重=double.parse(input2);
});
}
},
),),
尺寸箱(高度:10,),
升起的按钮(
填充:来自LTRB(20,5,20,5)的边缘设置,
onPressed:()异步{
等待计算(身高、体重);
//return Calculation.info1???//我不知道如何从计算函数中获取info1
},
颜色:颜色。琥珀色[900],
子:文本(
“计算”,
样式:TextStyle(
颜色:颜色,白色,
尺寸:30,
字母间距:2.0,
),
),
),
尺寸箱(高度:20,),
文本('结果:$height,$weight'),
//Text('Calculation.info1'),//我不知道如何在文本框中显示信息。
],
),
),
),
),
);
}
}
计算功能
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:math';
void Calculation(height,weight) {
double squarevalue = pow(height, 2);
double newsquare = squarevalue / 10000;
String info1="";
print(newsquare);
double value = weight / newsquare;
print(value);
// return value.toString();
if (value < 18.5) {
print("your weight is less than your ideal weight.");
// setState(() => info1="your weight is less than your ideal weight."); //i do not know how to set
// info1 to a new text
// return info1;
}
if (value > 25) {
if (value > 30) {
print("your weight is more than your ideal weight, your health is under risk.");
// info1="your weight is more than your ideal weight, your health is under risk.";
}
else {
print("your weight is more than your ideal weight.");
// info1="your weight is more than your ideal weight.";
}
}
else {
print("you are at your ideal weight.");
// info1="you are at your ideal weight.";
}
}
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
导入“dart:math”;
空隙率计算(高度、重量){
双平方值=功率(高度,2);
双新闻方=平方值/10000;
字符串info1=“”;
印刷品(新闻广场);
双值=重量/新闻方;
印刷品(价值);
//返回值.toString();
String Calculation(height,weight) {
....// your code with all conditions
return "you are at your ideal weight."
}
onPressed: () async{
String info = await Calculation(height, weight);
setState(){
infoDisplayedInText = info;
}
},