【问题标题】:Get Value of Selected DropdownMenuItem in Flutter获取 Flutter 中选中的 DropdownMenuItem 的值
【发布时间】:2018-08-31 23:42:40
【问题描述】:

我正在尝试使用ListView.builder 构建多个DropdownButton,次数与用户单击浮动操作按钮的次数一样多

new FloatingActionButton(
          onPressed: () {
            setState(() {
              counter++;
            });
          },
          child: new Icon(Icons.add),
      )

new ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                return buildfields(index); },
              itemCount: counter,
              scrollDirection: Axis.vertical,
            )

new DropdownButton<String>(
            onChanged: (String value) { setState((){
              setUn();
              _unit = value;
            });
            },
            hint: new Text('Course Unit'),
            value: _unit,
            items: <String>["1", "2", "3", "4", "5"].map((String value) {
              return new DropdownMenuItem<String>(
                value: value,
                child: new Text(value),
              );
            }).toList(),
          )

问题是这样的,当用户生成多个DropdownButton 并选择一个值时,每个其他生成的DropdownButton 将其值更改为新选择的值。如何为每个生成的DropdownButton 设置唯一的 id?

【问题讨论】:

  • 将值存储在列表或映射中,以便能够存储多个值。如果您只有一个变量 (_unit) 并且所有下拉菜单都使用该变量,则它们都将显示相同的选定值。
  • @Günter,我是 Flutter 的新手,你能用 sn-p 指导我吗?

标签: dart flutter


【解决方案1】:

尝试使用 ListView.Builder 和一个列表来保存值。

class MultipleDropDownPage extends StatefulWidget {
  MultipleDropDownPage({Key key}) : super(key: key);

  @override
  _MultipleDropDownPageState createState() => new _MultipleDropDownPageState();
}

class _MultipleDropDownPageState extends State<MultipleDropDownPage> {
  List<String> selectedValues;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    selectedValues = [];
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text('Multi Drop'),
      ),
      body: new ListView.builder(
        itemCount: selectedValues.length,
        itemBuilder: (context, index) {
          return new DropdownButton<String>(
            onChanged: (String value) {
              setState(() {
                selectedValues[index] = value;
              });
            },
            hint: new Text('Course Unit'),
            value: selectedValues[index],
            items: <String>["1", "2", "3", "4", "5"].map((String value) {
              return new DropdownMenuItem<String>(
                value: value,
                child: new Text(value),
              );
            }).toList(),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            selectedValues.add(null);
          });
        },
      ),
    );
  }
}

【讨论】:

    【解决方案2】:
    List<DropDownMenuButton> listDropdownMenu = new List<DropDownMenuButton>;
    List<String> listValue = new List<String>;
    
    @Override
    initState(){
    listDropdownMenuBtn = getDropDownList();
    listValue = getStringList();
    }
    
       //Creating list of DropDownMenu
    List<DropdownMenuButton> getDropDownList(){
    var localList = new List<DropdownMenuButton>();
      //You can set your own max here
    for(int a = 0;i<10; i++){
     localList.add(
     new DropdownButton<String>(
            onChanged: (String value) { setState((){
             listValue[i] = value;})
            },
            hint: new Text('Course Unit'),
            value: _unit,
            items: <String>["1", "2", "3", "4", "5"].map((String value) {
              return new DropdownMenuItem<String>(
                value: value,
                child: new Text(value),
              );
            }).toList(),
          )
         ))}
               return localList;
      }
    List<String> getStringList{
     var localList = new List<String>();
    for(int i=0,i<10, i++){
      localList.add("");
    }
       return localList;
    }}
    

    然后你可以像这样在 buildFieldIndex 中添加 DropdomMenuButton

    listDropdownMenu[index];
    

    希望对你有所帮助;

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2021-10-02
      • 2022-01-23
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 2021-10-31
      • 2022-07-06
      相关资源
      最近更新 更多