作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要你的帮助,
我想将来自不同文本字段的值相乘。我面临着 *
不能与 TexteditingController()
一起使用的问题:
void _calculation() {
_volume = lenCon * widCon * higCon;
print(_volume);
}
我之前是否需要将 lenCon
之类的变量转换为 double?我怎样才能做到这一点? var lenConInt = double.parse(lenCon);
不起作用。
RaisedButton 必须执行 _calculation
并用适当的值更改文本。我需要 StatlessWidget 还是 SetState?
提前致谢!
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var _volume = 0;
// var _lenght;
// var _width;
// var _hight;
void _calculation() {
_volume = lenCon * widCon * higCon;
print(_volume);
}
final lenCon = new TextEditingController();
final widCon = new TextEditingController();
final higCon = new TextEditingController();
// var lenConInt = double.parse(lenCon);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: lenCon,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'Länge',
),
),
TextField(
controller: widCon,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'Breite',
),
),
TextField(
controller: higCon,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'Höhe',
),
),
RaisedButton(
onPressed: (_calculation),
child: Text('Berechnen'),
),
Text('Your Volume is: $_volume'),
],
),
),
),
);
}
}
最佳答案
试试这个,让它发挥作用:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _volume;
@override
initState(){
_volume = 0;
}
void _calculation() {
setState((){
_volume = int.parse(lenCon.text) * int.parse(widCon.text) * int.parse(higCon.text);
},
);
print(_volume);
}
final lenCon = TextEditingController();
final widCon = TextEditingController();
final higCon = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: lenCon,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'Länge',
),
),
TextField(
controller: widCon,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'Breite',
),
),
TextField(
controller: higCon,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'Höhe',
),
),
RaisedButton(
onPressed: (_calculation),
child: Text('Berechnen'),
),
Text('Your Volume is: $_volume'),
],
),
),
),
);
}
}
首先,您需要在字符串和整数之间进行某种类型的转换(如果需要,甚至可以是 double )。其次,您应该将生成的 _volume
置于一个状态中,以便在您更新状态时(通过 setState
)刷新 UI。尽量避免使用 var
并改用具体类型。从长远来看,它将对您有所帮助。
关于Flutter - 将来自不同文本字段的值相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65073200/
我是一名优秀的程序员,十分优秀!