简体   繁体   English

如何根据 Flutter 中第一个 DropdownButton 的选择加载第二个 DropdownButton?

[英]How do I load a second DropdownButton based on selection from first DropdownButton in Flutter?

I have two dropdowns.我有两个下拉菜单。 Now i want to show secocnd dropdown only when first one is selected otherwise it should be hide.how can i do that in this code please anyone help me.现在我只想在选择第一个下拉菜单时才显示第二个下拉菜单,否则它应该是隐藏的。我该如何在这段代码中做到这一点,请任何人帮助我。

How can I hide/show widgets on basis of dropdown selection如何根据下拉选择隐藏/显示小部件

'How can I hide second dropdown until first is choosen?'   

    
       
      @override
      Widget build(BuildContext context) {
        loadDatalistDropexpensetype();
        loadDatalistDropexpensetype1();
        return new Scaffold(
           appBar: AppBar(   'appbar'
            title: Text("DropDown Testing 2"),
          ),
          body: new Column(
            children: <Widget>[
              new DropdownButton(
                items: listDropexpensetype, 'item which are mentioned in function'
                  value: select,
                  hint: Text("select option"),
                  onChanged: (value){
                    print(value.toString());
                    setState(() {
                      select=value;
                    });
                  }
              ),
              Visibility(
                visible: tcVisibility,
                child: new DropdownButton(   'this should onlt show on selection of first'
                  items: listDropexpensetype1,
                  value: selectedexpensetype,
                  hint: Text("select option"),
                  onChanged: (value){
                    print(value.toString());
                    setState(() {
                      selectedexpensetype=value;
                    });
                  }
                ),
              ),
            ],
          ),
          
        );
      }
    }

You can store a temp variable outside of your build function.您可以在构建 function 之外存储临时变量。 For example,例如,

String firstDropDownData = "";

In the onChange() function of your first drop-down, simply update the value of "firstDropDownData" and store something relative in it.在您的第一个下拉列表的 onChange() function 中,只需更新“firstDropDownData”的值并在其中存储相关内容。 Once you'll have something in "firstDropDownData", your second dropdown will be rendered automatically in the UI.一旦你在“firstDropDownData”中有一些东西,你的第二个下拉菜单将在 UI 中自动呈现。

Consider wrapping your second drop-down with the following line.考虑使用以下行包装您的第二个下拉菜单。

        firstDropDownData != "" ? DropdownButton(
              items: listDropexpensetype1,
              value: selectedexpensetype,
              hint: Text("select option"),
              onChanged: (value){
                print(value.toString());
                setState(() {
                  selectedexpensetype=value;
                });
              }
            ) : Container()

UPDATE:更新:

On your request, here's a full demo code:根据您的要求,这是一个完整的演示代码:

String firstDropDownData = "";


@override
      Widget build(BuildContext context) {
        loadDatalistDropexpensetype();
        loadDatalistDropexpensetype1();
        return Scaffold(
           appBar: AppBar(   'appbar'
            title: Text("DropDown Testing 2"),
          ),
          body: Column(
            children: <Widget>[
              DropdownButton(
                items: listDropexpensetype, 
                  value: select,
                  hint: Text("select option"),
                  onChanged: (value){
                    print(value.toString());
                    setState(() {
                      firstDropDownData = value;
                    });
                  }
              ),
              firstDropDownData != "" ? DropdownButton(  
                  items: listDropexpensetype1,
                  value: selectedexpensetype,
                  hint: Text("select option"),
                  onChanged: (value){
                    print(value.toString());
                    setState(() {
                      selectedexpensetype=value;
                    });
                  }
                ) : Container(),
            ],
          ),
          
        );
      }
    }

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

相关问题 如何根据第一个DropdownButton的选择加载第二个DropdownButton - How do I load a second DropdownButton based on selection from first DropdownButton 如何以编程方式清除 Flutter DropdownButton? - How do I clear a Flutter DropdownButton programmatically? 如何将 DropdownButton 的第一项设置为第一个 API 的值,而其余项设置为 Flutter 中第二个 API 的值? - How to set the first items of DropdownButton to be value from first API while the rest of the items to be value from second API in Flutter? 如何在 flutter DropDownButton 中设置提示动态? - How do I set hint dynamic in flutter DropDownButton? 如何在 flutter 中将 DropdownButton 小部件转换为列表视图小部件 - How do I convert a DropdownButton widget into a listview widget in flutter 数据表中的 Flutter DropdownButton,列表中的 DropdownButton 选项 - Flutter DropdownButton in DataTable, DropdownButton options from list 如何加载 <?, ?> 将json文件输入DropdownButton - How do I load <?, ?> type json file into DropdownButton 如何从 Flutter 中的另一个小部件更改 DropdownButton 值? - How can I change DropdownButton values from another widget in Flutter? Flutter:如何使用 DropDownButton? - Flutter: how to use DropDownButton? 如何在 Flutter 中显示 DropDownButton? - How to show DropDownButton in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM