Flutter - 从 TopDownMenu 中选择带有数字的文本,并将此数字用于计算
Flutter - Choose text from TopDownMenu with a number attached to it and take this number into a calculation
我再次在这里请求您的支持。我不知道该怎么做。
我有一个代码可以将文本字段中的值相乘,现在我想将下拉菜单中的值添加到计算中。
你能告诉我怎么做吗?我已经列出了这些值:
final _materials = const [
{
'Type': [
{'material': 'Stahl', 'dichte': 7.87},
{'material': 'Zamak', 'dichte': 6.7},
{'material': 'Aluminium', 'dichte': 2.7},
],
}
];
我不知道这是不是正确的格式。目标是,如果你从下拉列表中选择“Stahl”:
DropdownButton(items: null, onChanged: null),
应将值“dichte”(7.87) 放入计算中:
void _calculation() {
setState(
() {
_volume = int.parse(lenCon.text) *
int.parse(widCon.text) *
int.parse(higCon.text) *
'dichte';
},
);
print(_volume);
}
完整代码如下:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
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) *
'dichte';
},
);
print(_volume);
}
final lenCon = TextEditingController();
final widCon = TextEditingController();
final higCon = TextEditingController();
final _materials = const [
{
'Type': [
{'material': 'Stahl', 'dichte': 7.87},
{'material': 'Zamak', 'dichte': 6.7},
{'material': 'Aluminium', 'dichte': 2.7},
],
}
];
@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',
),
),
DropdownButton(items: null, onChanged: null),
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(
debugShowCheckedModeBanner: false,
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> {
double _volume = 0;
double dropValue = 0.0;
var selectedValue;
void _calculation() {
print(lenCon.text +
" " +
widCon.text +
" " +
higCon.text +
" " +
dropValue.toString());
setState(
() {
_volume = int.parse(lenCon.text) *
int.parse(widCon.text) *
int.parse(higCon.text) *
dropValue;
},
);
print(_volume);
}
final lenCon = TextEditingController();
final widCon = TextEditingController();
final higCon = TextEditingController();
final _materialsTypes = [
{'material': 'Stahl', 'dichte': 7.87},
{'material': 'Zamak', 'dichte': 6.7},
{'material': 'Aluminium', 'dichte': 2.7}
];
@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',
),
),
DropdownButton(
hint: new Text("Select a Material"),
value: selectedValue,
items: _materialsTypes.map((value) {
return new DropdownMenuItem(
value: value["dichte"],
child: new Text(value["material"]),
);
}).toList(),
onChanged: (item) {
setState(() {
try {
dropValue = item;
selectedValue = item;
} catch (e) {
print(e);
}
});
print(item);
}),
// DropdownButton(items: null, onChanged: null),
RaisedButton(
onPressed: (_calculation),
child: Text('Berechnen'),
),
Text('Your Volume is: $_volume'),
],
),
),
),
);
}
}
兄弟,这是你的解决方案
我再次在这里请求您的支持。我不知道该怎么做。
我有一个代码可以将文本字段中的值相乘,现在我想将下拉菜单中的值添加到计算中。
你能告诉我怎么做吗?我已经列出了这些值:
final _materials = const [
{
'Type': [
{'material': 'Stahl', 'dichte': 7.87},
{'material': 'Zamak', 'dichte': 6.7},
{'material': 'Aluminium', 'dichte': 2.7},
],
}
];
我不知道这是不是正确的格式。目标是,如果你从下拉列表中选择“Stahl”:
DropdownButton(items: null, onChanged: null),
应将值“dichte”(7.87) 放入计算中:
void _calculation() {
setState(
() {
_volume = int.parse(lenCon.text) *
int.parse(widCon.text) *
int.parse(higCon.text) *
'dichte';
},
);
print(_volume);
}
完整代码如下:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
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) *
'dichte';
},
);
print(_volume);
}
final lenCon = TextEditingController();
final widCon = TextEditingController();
final higCon = TextEditingController();
final _materials = const [
{
'Type': [
{'material': 'Stahl', 'dichte': 7.87},
{'material': 'Zamak', 'dichte': 6.7},
{'material': 'Aluminium', 'dichte': 2.7},
],
}
];
@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',
),
),
DropdownButton(items: null, onChanged: null),
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(
debugShowCheckedModeBanner: false,
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> {
double _volume = 0;
double dropValue = 0.0;
var selectedValue;
void _calculation() {
print(lenCon.text +
" " +
widCon.text +
" " +
higCon.text +
" " +
dropValue.toString());
setState(
() {
_volume = int.parse(lenCon.text) *
int.parse(widCon.text) *
int.parse(higCon.text) *
dropValue;
},
);
print(_volume);
}
final lenCon = TextEditingController();
final widCon = TextEditingController();
final higCon = TextEditingController();
final _materialsTypes = [
{'material': 'Stahl', 'dichte': 7.87},
{'material': 'Zamak', 'dichte': 6.7},
{'material': 'Aluminium', 'dichte': 2.7}
];
@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',
),
),
DropdownButton(
hint: new Text("Select a Material"),
value: selectedValue,
items: _materialsTypes.map((value) {
return new DropdownMenuItem(
value: value["dichte"],
child: new Text(value["material"]),
);
}).toList(),
onChanged: (item) {
setState(() {
try {
dropValue = item;
selectedValue = item;
} catch (e) {
print(e);
}
});
print(item);
}),
// DropdownButton(items: null, onChanged: null),
RaisedButton(
onPressed: (_calculation),
child: Text('Berechnen'),
),
Text('Your Volume is: $_volume'),
],
),
),
),
);
}
}
兄弟,这是你的解决方案