简体   繁体   English

flutter 中的多重依赖下拉列表

[英]Multiple Dependent dropdown in flutter

I am trying to build multiple dependent dropdown on flutter. The second one depend on 1st one.我正在尝试在 flutter 上构建多个依赖下拉列表。第二个依赖于第一个。 here is the code to the dropdown I have implemented.这是我实现的下拉菜单的代码。

Container(
            child: new DropdownButton<String>(
              underline: SizedBox(
                height: 1,
              ),
              hint: new Text("Select Faculty"),
              value: facultyId,
              items: faculty.map((item) {
                return new DropdownMenuItem(
                  child: new Text(item['name']),
                  value: item['id'].toString(),
                );
              }).toList(),
              onChanged: (faculty == null)
                  ? null
                  : (String newValue) {
                      setState(() {
                        filteredbatch.clear();
                        facultyId = newValue;
                        for (var item in allbatch) {
                          if (item['facultyId'].toString() == newValue){
                          filteredbatch.add(item);
                            disableBatch = false;
                          }
                        }
                      });
                      print(facultyId);
                    },
            ),
          ),
          Container(
            child: new DropdownButton<String>(
                underline: SizedBox(
                  height: 1,
                ),
                disabledHint: new Text("Select Faculty First"),
                hint: Text("Select Batch"),
                value: batchId,
                onChanged: disableBatch
                    ? null
                    : (String newValue) {
                        setState(() {
                          batchId = newValue;
                          disableSection = false;
                        });
                        print(batchId);
                      },
                items: filteredbatch.map((item) {
                  return DropdownMenuItem(
                    child: Text(item['name']),
                    value: item['id'].toString(),
                  );
                }).toList()),
          ),

Whenever I select one dropdown item from first one, it enables 2nd dropdown and lets me to select an item from that dropdown.每当我 select 第一个下拉项目时,它会启用第二个下拉菜单并让我从该下拉菜单中选择一个项目 select 。 And when I select an item from 2nd dropdown and go back to change first dropdown, it throws error that dropdown requires one item with respective value.当我 select 第二个下拉列表中的一个项目和 go 返回更改第一个下拉列表时,它会抛出错误,下拉列表需要一个具有相应值的项目。 0 or 2 found.找到 0 或 2 个。 I am lost here.我在这里迷路了。 How do I resolve this error?我该如何解决这个错误?

What is going on here is quite simple.这里发生的事情很简单。 Let's say "allbatch" has these values:假设“allbatch”具有以下值:

faculty: foo , which has batchids: foo1, foo2, foo3教员: foo ,它有 batchids: foo1, foo2, foo3

faculty: bar , which has batchids: bar1, bar2, bar3. Faculty: bar ,它有 batchids: bar1, bar2, bar3。

When you select foo in the 1st dropdown a new "filteredbatch" is created and it only contains foo1,foo2,foo3.当您在第一个下拉列表中选择 foo 时,会创建一个新的“filteredbatch”,它只包含 foo1、foo2、foo3。 You then select foo3 in your 2nd dropdown and everything is still working fine...然后,您在第二个下拉列表中选择 foo3,一切仍然正常...

BUT when you change your 1st dropdown to bar, then "filteredbatch" only contains:bar1, bar2, bar3 and your second dropdown value is still set to foo3 which can not be found in the newly generated "filteredbatch", and then you get that error you are seeing.但是,当您将第一个下拉列表更改为 bar 时,“filteredbatch”仅包含:bar1、bar2、bar3 并且您的第二个下拉值仍设置为 foo3,而在新生成的“filteredbatch”中找不到该值,然后您会得到你看到的错误。

To fix this simply set batchId to null before changing the "filteredbatch" in your 1st dropdown onChanged method:要解决此问题,只需在更改第一个下拉菜单 onChanged 方法中的“filteredbatch”之前将 batchId 设置为 null:

  setState(() {
    //fix
    batchId = null;
    //end of fix
    filteredbatch.clear();
    facultyId = newValue;
    for (var item in allbatch) {
      if (item['facultyId'].toString() == newValue){
      filteredbatch.add(item);
        disableBatch = false;
      }
    }
  });

Your 2nd dropdown will revert back to hint text and the app user will have to select a new batchId.您的第二个下拉列表将恢复为提示文本,应用程序用户必须选择一个新的 batchId。

If you have any more questions feel free to ask.如果您有更多问题,请随时提出。

Flutter Dropdown Button FormField Dependent Flutter 下拉按钮 FormField 依赖

List<String> dataList = ["A", "B", "C"];
    List<String> dataListA = ["A1", "A2", "A3", "A4", "A5"];
    List<String> dataListB = ["B1", "B2", "B3", "B4", "B5"];
    List<String> dataListC = ["C1", "C2", "C3", "C4", "C5"];
    
    String? valueItem;
    List<String> listItem = [];


// DropdownButtonFormField
Container(
  margin: const EdgeInsets.only(bottom: p20),
  child: DropdownButtonFormField<String>(
  decoration: InputDecoration(
  labelText: 'Data list',
  labelStyle: const TextStyle(),
  border: OutlineInputBorder(
  borderRadius: BorderRadius.circular(5.0)),
  contentPadding: const EdgeInsets.only(left: 5.0),),
  value: valueItem,
  isExpanded: true,
  items: dataList.map((String value) {
   return DropdownMenuItem<String>(
   value: value,
  child: Text(value),);
 }).toSet().toList(),
  onChanged: (value) {
   setState(() {
         valueItem= value!;
       if (valueItem=="A") {
          listItem= dataListA;
      } else if (valueItem== "B") {
         listItem= dataListB;
      } else if (valueItem== "C") {
      listItem= dataListC;
    }
  });
},

), ), ), ),

// Second DropdownButtonFormField
Container(
  margin: const EdgeInsets.only(bottom: p20),
  child: DropdownButtonFormField<String>(
  decoration: InputDecoration(
  labelText: 'List dependent',
  labelStyle: const TextStyle(),
  border: OutlineInputBorder(
  borderRadius: BorderRadius.circular(5.0)),
  contentPadding: const EdgeInsets.only(left: 5.0),),
  value: ListItem,
  isExpanded: true,
  items: listItem.map((String value) {
   return DropdownMenuItem<String>(
    value: value,
    child: Text(value),
  );}).toSet().toList(),
  onChanged: (value) {
   setState(() {
     your_value = value!;
  });
 },

), ), ), ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM