Flutter Dart显示带有循环的DropdownButton选项

时间:2019-12-17 01:05:52

标签: flutter dart

我正在开发一个Flutter应用程序,并且我想拥有一个DropdownButton,它以1.25的增量显示数字1-99。到目前为止,我正在手动将它们全部写出,但是有没有办法创建一个循环来做到这一点呢?这是我的代码:

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,
        ),

我了解循环,但不确定如何执行。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您可以使用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();

编辑:这是对我有用的完整小部件

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,
    );
  }
}

答案 1 :(得分:0)

这适用于循环下拉

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"),