gpt4 book ai didi

Flutter - 将来自不同文本字段的值相乘

转载 作者:行者123 更新时间:2023-12-04 13:11:09 25 4
gpt4 key购买 nike

我需要你的帮助,

我想将来自不同文本字段的值相乘。我面临着 * 不能与 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com