在Flutter中获取选定的DropdownMenuItem的值

时间:2018-08-31 13:38:49

标签: dart flutter

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

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?

2 个答案:

答案 0 :(得分:0)

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];

希望有帮助;

答案 1 :(得分:0)

尝试使用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);
          });
        },
      ),
    );
  }
}