[英]Flutter Dart showing DropdownButton options with a loop
I am working on a flutter application, and I want to have a DropdownButton that displays numbers 1-99 in increments of .25.我正在开发一个颤振应用程序,我想要一个 DropdownButton 以 0.25 的增量显示数字 1-99。 So far I am manually writing all of them out, but is there any way to create a loop that does this?到目前为止,我正在手动将它们全部写出来,但是有没有办法创建一个循环来执行此操作? Here is my code:这是我的代码:
DropdownButton<String>(
items: [
DropdownMenuItem(
value: "1",
child: Text("1"),
),
DropdownMenuItem(
value: "1.25",
child: Text("1.25"),
),
DropdownMenuItem(
value: "1.5",
child: Text("1.5"),
),
DropdownMenuItem(
value: "1.75",
child: Text("1.75"),
),
DropdownMenuItem(
value: "2",
child: Text("2"),
),
DropdownMenuItem(
value: "2.25",
child: Text("2.25"),
),
DropdownMenuItem(
value: "2.5",
child: Text("2.5"),
),
DropdownMenuItem(
value: "2.75",
child: Text("2.75"),
),
DropdownMenuItem(
value: "3",
child: Text("3"),
),
DropdownMenuItem(
value: "3.25",
child: Text("3.25"),
),
DropdownMenuItem(
value: "3.5",
child: Text("3.5"),
),
DropdownMenuItem(
value: "3.75",
child: Text("3.75"),
),
DropdownMenuItem(
value: "4",
child: Text("4"),
),
DropdownMenuItem(
value: "4.25",
child: Text("4.25"),
),
DropdownMenuItem(
value: "4.5",
child: Text("4.5"),
),
DropdownMenuItem(
value: "4.75",
child: Text("4.75"),
),
DropdownMenuItem(
value: "5",
child: Text("5"),
),
],
onChanged: (value) {
setState(() {
_value2 = value;
});
},
//new code
hint: Text("#"),
value: _value2,
),
I understand loops but am not sure how to do this.我了解循环,但不确定如何执行此操作。
Thanks for your help.谢谢你的帮助。
You can use List.generate to generate a list of values, and then map that into a list of dropdown menu items:您可以使用 List.generate 生成值列表,然后将其映射到下拉菜单项列表中:
List<String> doubleList = List<String>.generate(393, (int index) => '${index * .25 + 1}');
List<DropdownMenuItem> menuItemList = doubleList.map((val) => DropdownMenuItem(value: val, child: Text(val))).toList();
edit: here's the complete widget that worked for me编辑:这是对我有用的完整小部件
class DropdownList extends StatefulWidget {
@override
_DropdownListState createState() => _DropdownListState();
}
class _DropdownListState extends State<DropdownList> {
String selected;
@override
Widget build(BuildContext context) {
List<String> doubleList =
List<String>.generate(393, (int index) => '${index * .25 + 1}');
List<DropdownMenuItem> menuItemList = doubleList
.map((val) => DropdownMenuItem(value: val, child: Text(val)))
.toList();
return DropdownButton(
value: selected,
onChanged: (val) => setState(() => selected = val),
items: menuItemList,
);
}
}
this would work for the loop dropdown这适用于循环下拉列表
String _dropDownValueInch;
DropdownButton( underline: SizedBox(),
hint: _dropDownValueInch == null
? Text(
'0',
textAlign: TextAlign.center,
)
: Text(
_dropDownValueInch,
style: TextStyle(
color: Colors.black),
),
// isExpanded: true,
iconSize: 30.0,
style: TextStyle(color: Colors.black),
items: List<String>.generate(100,
(int index) => '${index + 1}').map(
(val) {
return DropdownMenuItem<String>(
value: val,
child: Text(val),
);
},
).toList(),
onChanged: (val) {
setState(
() {
_dropDownValueInch = val;
},
);
},
),
Text("select a number"),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.