[英]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.