在Flutter中获取所选DropdownMenuItem的值

问题描述 投票:0回答:2

我正在尝试使用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并选择One的值时,每个其他生成的DropdownButton将其值更改为新选择的值。如何为每个生成的DropdownButton设置唯一ID?

dart flutter
2个回答
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);
          });
        },
      ),
    );
  }
}

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

希望它有所帮助;

© www.soinside.com 2019 - 2024. All rights reserved.