[英]Returning a value from class in flutter after Raisedbutton
I am building my first app for calculating values I need for work.我正在构建我的第一个应用程序来计算我工作所需的值。 I already have this working in Python, but that is really different.我已经在 Python 中进行了这项工作,但这真的很不一样。 I have a textformfield where I put in 'the data I need.我有一个 textformfield,我在其中输入了“我需要的数据”。 With succes I call a class from 'onPress' button and pass the data.成功后,我从“onPress”按钮调用一个类并传递数据。 After calculation I need the calculated value to show in a textfield (spev).计算后,我需要将计算值显示在文本字段 (spev) 中。 How can I get the value out of the class 'Buisber' back to the textfield.如何将“Buisber”类的值返回到文本字段。 I am searching for days, but could not find it.我找了几天,但找不到。 I am sure I read over it somewhere, but just did not understand.我确定我在某处读过它,但就是不明白。 Here a stripped version:这是一个剥离版本:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Buis(),
));
}
class Buis extends StatelessWidget {
@override
Widget build(BuildContext context) {
String spev = '0';
TextEditingController kapController = TextEditingController(text: '10.60');
return Scaffold(
appBar: AppBar(
title: Center(
child: Text("Power Outcome"),
),
),
body: Center(
child: Container(
decoration: BoxDecoration(
),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(height: 20),
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
// Calculate
Center(
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
onPressed: () {
Buisber(
kapController.text);
},
color: Colors.blue,
textColor: Colors.white,
child: Text("Calculate".toUpperCase(),
style: TextStyle(fontSize: 14)),
),
),
// Outcome Spev
Container(
alignment: Alignment(0, 0),
width: 80,
height: 30,
decoration: BoxDecoration(
borderRadius: BorderRadius.horizontal(
left: Radius.circular(40),
right: Radius.circular(40),
),
border: Border.all(width: 1.0),
),
child: Text(
'$spev',
style: TextStyle(
fontSize: 15,
),
),
),
SizedBox(height: 10),
],
),
],
),
),
),
),
);
}
}
class Buisber {
Buisber(kap) {
var kap1 = double.parse(kap);
print(kap1);
var spev = 1.7 * (50 / kap1);
spev = num.parse(spev.toStringAsFixed(2));
//testing (print works)
//how to send back Outcome Spev
print("Spev is " '$spev');
}
}
@Pim, why not just create a function instead of class and receive the value inside Stateful widget? @Pim,为什么不创建一个函数而不是类并在 Stateful 小部件中接收值? Then you can refresh the state to update the value like so,然后您可以刷新状态以更新值,如下所示,
void main() {
runApp(new Buis());
}
class Buis extends StatefulWidget {
@override
BuisState createState() => BuisState();
}
class BuisState extends State<Buis> {
var value = '';
@override
Widget build(BuildContext context) {
String spev = '0';
TextEditingController kapController = TextEditingController(text: '10.60');
return MaterialApp(home: Scaffold(
appBar: AppBar(
title: Center(
child: Text("Power Outcome"),
),
),
body: Center(
child: Container(
decoration: BoxDecoration(
),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(height: 20),
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
// Calculate
Center(
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
onPressed: () {
value = getValue(kapController.text).toString();
setState(() {});
},
color: Colors.blue,
textColor: Colors.white,
child: Text("Calculate".toUpperCase(),
style: TextStyle(fontSize: 14)),
),
),
// Outcome Spev
Container(
alignment: Alignment(0, 0),
width: 80,
height: 30,
decoration: BoxDecoration(
borderRadius: BorderRadius.horizontal(
left: Radius.circular(40),
right: Radius.circular(40),
),
border: Border.all(width: 1.0),
),
child: Text(
value.toString(),
style: TextStyle(
fontSize: 15,
),
),
),
SizedBox(height: 10),
],
),
],
),
),
),
),
)
);
}
}
// This can be within your BuisState
getValue (kap) {
var kap1 = double.parse(kap);
print(kap1);
var spev = 1.7 * (50 / kap1);
spev = num.parse(spev.toStringAsFixed(2));
//testing (print works)
//how to send back Outcome Spev
print("Spev is " '$spev');
return spev;
}
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.