Drop down menu 从第一个下拉列表的值中获取第二个下拉列表
我试着有两个下拉按钮和一个升起的按钮。第一个下拉按钮是国家列表,第二个下拉按钮是国家列表,例如,当用户从第一个下拉列表中选择印度时,则从印度各州填充第二个下拉列表,或者如果用户在第一个下拉列表中选择美国,则第二个下拉列表由美国各州填充,最后是一个凸起的按钮,该按钮将根据该州打开指定页面选中 我已经制作了一个国家/地区的下拉按钮-a、B、C、D,但我无法根据第一个下拉列表中选择的国家/地区填充第二个下拉按钮,也无法根据国家/地区的选择打开新的单独页面Drop down menu 从第一个下拉列表的值中获取第二个下拉列表,drop-down-menu,flutter,dart,Drop Down Menu,Flutter,Dart,我试着有两个下拉按钮和一个升起的按钮。第一个下拉按钮是国家列表,第二个下拉按钮是国家列表,例如,当用户从第一个下拉列表中选择印度时,则从印度各州填充第二个下拉列表,或者如果用户在第一个下拉列表中选择美国,则第二个下拉列表由美国各州填充,最后是一个凸起的按钮,该按钮将根据该州打开指定页面选中 我已经制作了一个国家/地区的下拉按钮-a、B、C、D,但我无法根据第一个下拉列表中选择的国家/地区填充第二个下拉按钮,也无法根据国家/地区的选择打开新的单独页面 The code- List<Stri
The code-
List<String> _locations = ['A', 'B', 'C', 'D']; // Option 2
String _selectedLocation;
@override
Widget build(BuildContext context) {
return SizedBox(
height: MediaQuery.of(context).size.height * 0.55,
child: Scaffold(
body:Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Select a Country",style: TextStyle(fontWeight: FontWeight.bold),),
DropdownButton(
hint: Text('Please choose a Country'),
value: _selectedLocation,
onChanged: (newValue) {
setState(() {
_selectedLocation = newValue;
});
},
items: _locations.map((location) {
return DropdownMenuItem(
child: new Text(location),
value: location,
);
}).toList(),
),
new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text("Select a State",style: TextStyle(fontWeight: FontWeight.bold),),
DropdownButton(
hint: Text('Please choose a State'),
value: _selectedLocation,
onChanged: (newValue) {
setState(() {
_selectedLocation = newValue;
});
},
items: _locations.map((location) {
return DropdownMenuItem(
child: new Text(location),
value: location,
);
}).toList(),
),
],
),
new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new RaisedButton(
padding: const EdgeInsets.all(8.0),
textColor: Colors.white,
color: Colors.blue,
// onPressed: ,
child: new Text("OK"),
),
],
)
],
)
)
)
);
代码-
列出_位置=['A'、'B'、'C'、'D'];//选择2
字符串\u选择的位置;
@凌驾
小部件构建(构建上下文){
返回大小框(
高度:MediaQuery.of(上下文).size.height*0.55,
孩子:脚手架(
正文:中(
子:新列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
Text(“选择一个国家”,样式:TextStyle(fontWeight:fontWeight.bold),),
下拉按钮(
提示:文本('请选择一个国家'),
值:_selectedLocation,
一旦更改:(newValue){
设置状态(){
_selectedLocation=newValue;
});
},
项目:_locations.map((位置){
返回下拉菜单项(
子项:新文本(位置),
价值:位置,
);
}).toList(),
),
新专栏(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
文本(“选择一个状态”,样式:TextStyle(fontWeight:fontWeight.bold),),
下拉按钮(
提示:文本('请选择一个状态'),
值:_selectedLocation,
一旦更改:(newValue){
设置状态(){
_selectedLocation=newValue;
});
},
项目:_locations.map((位置){
返回下拉菜单项(
子项:新文本(位置),
价值:位置,
);
}).toList(),
),
],
),
新专栏(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
新升起的按钮(
填充:常数边集全部(8.0),
textColor:Colors.white,
颜色:颜色,蓝色,
//按下按钮:,
子项:新文本(“确定”),
),
],
)
],
)
)
)
);
有人能帮我解决这个问题吗?。请提前感谢。您可以使用地图管理国家/地区的国家/地区基础,然后您可以使用该国家/地区导航用户 以下代码可能会帮助您解决问题 将此路线添加到
routes: {
'/abcPage': (context) => abcPage(),
'/defPage': (context) => defPage(),
},
主页:
import 'package:flutter/material.dart';
class Homestack extends StatefulWidget {
@override
_HomestackState createState() => _HomestackState();
}
class _HomestackState extends State<Homestack> {
List<String> _locations = ['A', 'B', 'C', 'D']; // Option 2
Map<String,String> _CountryState = {
"aa":"A",
"aaa":"A",
"BBB":"B",
"BB":"B",
"CC":"C",
"DDD":"D"
};
Map<String,String> _navigate = {
"aa":"/abcPage",
"BB":"/defPage"
};
List<String> _state =[];
String _selectedLocation;
String _selectedState;
@override
Widget build(BuildContext context) {
return SizedBox(
height: MediaQuery.of(context).size.height * 0.55,
child: Scaffold(
body:Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Select a Country",style: TextStyle(fontWeight: FontWeight.bold),),
DropdownButton(
key: Key("Country"),
hint: Text('Please choose a Country'),
value: _selectedLocation,
onChanged: (newValue) {
setState(() {
_CountryState.forEach((k,v){
print("${k} and ${v}" );
_state.clear();
if(v==newValue){
_state.add(k);
}
});
_selectedLocation = newValue;
});
},
items: _locations.map((location) {
return DropdownMenuItem(
child: new Text(location),
value: location,
);
}).toList(),
),
new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text("Select a State",style: TextStyle(fontWeight: FontWeight.bold),),
DropdownButton(
key: Key("state"),
hint: Text('Please choose a State'),
value: _selectedState,
onChanged: (newValue) {
setState(() {
_selectedState = newValue;
});
},
items: _state.map((location) {
return DropdownMenuItem(
child: new Text(location),
value: location,
);
}).toList(),
),
],
),
new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new RaisedButton(
padding: const EdgeInsets.all(8.0),
textColor: Colors.white,
color: Colors.blue,
onPressed: (){
Navigator.pushNamed(context,_navigate[_selectedState] );
},
child: new Text("OK"),
),
],
)
],
)
)
)
);
}
}
class abcPage extends StatefulWidget {
@override
_abcPageState createState() => _abcPageState();
}
class _abcPageState extends State<abcPage> {
@override
Widget build(BuildContext context) {
return Material(
child: Container(
color: Colors.green,
));
}
}
class defPage extends StatefulWidget {
@override
_defPageState createState() => _defPageState();
}
class _defPageState extends State<defPage> {
@override
Widget build(BuildContext context) {
return Container();
}
}
导入“包装:颤振/材料.省道”;
类Homestack扩展StatefulWidget{
@凌驾
_HomestackState createState()=>\u HomestackState();
}
类_HomestackState扩展状态{
列出_位置=['A'、'B'、'C'、'D'];//选项2
地图_CountryState={
“aa”:“A”,
“aaa”:“A”,
“BBB”:“B”,
“BB”:“B”,
“抄送”:“C”,
“DDD”:“D”
};
地图_导航={
“aa”:“/abcPage”,
“BB”:“/defPage”
};
列表_state=[];
字符串\u选择的位置;
字符串_selectedState;
@凌驾
小部件构建(构建上下文){
返回大小框(
高度:MediaQuery.of(上下文).size.height*0.55,
孩子:脚手架(
正文:中(
子:新列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
Text(“选择一个国家”,样式:TextStyle(fontWeight:fontWeight.bold),),
下拉按钮(
关键:关键(“国家”),
提示:文本('请选择一个国家'),
值:_selectedLocation,
一旦更改:(newValue){
设置状态(){
_CountryState.forEach((k,v){
打印(${k}和${v});
_state.clear();
if(v==newValue){
_添加(k);
}
});
_selectedLocation=newValue;
});
},
项目:_locations.map((位置){
返回下拉菜单项(
子项:新文本(位置),
价值:位置,
);
}).toList(),
),
新专栏(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
文本(“选择一个状态”,样式:TextStyle(fontWeight:fontWeight.bold),),
下拉按钮(
密钥:密钥(“状态”),
提示:文本('请选择一个状态'),
值:_selectedState,
一旦更改:(newValue){
设置状态(){
_selectedState=newValue;
});
},
项目:_state.map((位置){
返回下拉菜单项(
孩子:新的