Flutter 当其他下拉值在颤振中更改时,将下拉值重置为偏移0
我正在使用一个下拉小部件来显示来自它的多个drpdownFlutter 当其他下拉值在颤振中更改时,将下拉值重置为偏移0,flutter,flutter-layout,flutter-animation,Flutter,Flutter Layout,Flutter Animation,我正在使用一个下拉小部件来显示来自它的多个drpdown String CountryVal, StateVal; Widget _dropdown(List<String> _options, String selected){ return Container( padding: EdgeInsets.fromLTRB(15.0, 4.0, 15.0, 4.0), margin: EdgeInsets.fromLTRB(20.0, 7.0,
String CountryVal, StateVal;
Widget _dropdown(List<String> _options, String selected){
return Container(
padding: EdgeInsets.fromLTRB(15.0, 4.0, 15.0, 4.0),
margin: EdgeInsets.fromLTRB(20.0, 7.0, 20.0, 10.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
isExpanded: true,
hint: Text(
_options[0],
style: TextStyle(
color: Colors.blue,
fontSize: 18.0,
),
),
items: _options.map((String value){
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String captureSelected) {
setState(() {
if(selected == 'Country Dropdown'){
CountryVal = captureSelected;
} else if(selected == 'State Dropdown'){
StateVal = captureSelected;
}
});
},
value: selected == 'Country Dropdown' ? CountryVal : StateVal,
style: TextStyle(
color: Colors.blue,
fontSize: 18.0,
),
),
),
);
}
字符串CountryVal,StateVal;
小部件下拉列表(列表选项,选定字符串){
返回容器(
填充:来自LTRB(15.0,4.0,15.0,4.0)的边缘设置,
边距:LTRB(20.0,7.0,20.0,10.0)的边距集,
子项:DropdownButtonHideUnderline(
孩子:下拉按钮(
是的,
提示:文本(
_选项[0],
样式:TextStyle(
颜色:颜色,蓝色,
字体大小:18.0,
),
),
项目:_options.map((字符串值){
返回下拉菜单项(
价值:价值,
子项:文本(值),
);
}).toList(),
onChanged:(已选择字符串捕获){
设置状态(){
如果(所选==“国家/地区下拉列表”){
CountryVal=captureSelected;
}else if(已选==“状态下拉列表”){
StateVal=captureSelected;
}
});
},
值:selected==“国家/地区下拉列表”?CountryVal:StateVal,
样式:TextStyle(
颜色:颜色,蓝色,
字体大小:18.0,
),
),
),
);
}
我在不同的下拉列表中多次使用它
List<String> _country = ['- Select Country -', 'USA', 'UK'];
List<String> _state = ['- Select State -', 'New York', 'London'];
_dropdown(_country, 'Country Dropdown');
_dropdown(_state, 'State Dropdown');
List _country=['-选择国家-','USA','UK'];
列表_state=['-选择state-'、'纽约'、'伦敦';
_下拉列表(_country,'country dropdown');
_下拉列表(_state,“state dropdown”);
现在“国家下拉列表”上的值取决于“国家下拉列表”,当选择“国家下拉列表”值时,我需要将“国家下拉列表”的值重置为偏移0
我花了几个小时亲自搜索和尝试,但我能找到正确的答案。请提供帮助。您可以复制粘贴运行下面的完整代码
您可以设置
StateVal=_state[0]代码>选择国家/地区下拉列表值时。
代码片段
if (selected == 'Country Dropdown') {
CountryVal = captureSelected;
StateVal = _state[0];
}
工作演示
完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String CountryVal, StateVal;
List<String> _country = ['- Select Country -', 'USA', 'UK'];
List<String> _state = ['- Select State -', 'New York', 'London'];
Widget _dropdown(List<String> _options, String selected) {
return Container(
padding: EdgeInsets.fromLTRB(15.0, 4.0, 15.0, 4.0),
margin: EdgeInsets.fromLTRB(20.0, 7.0, 20.0, 10.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
isExpanded: true,
hint: Text(
_options[0],
style: TextStyle(
color: Colors.blue,
fontSize: 18.0,
),
),
items: _options.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String captureSelected) {
setState(() {
if (selected == 'Country Dropdown') {
CountryVal = captureSelected;
StateVal = _state[0];
} else if (selected == 'State Dropdown') {
StateVal = captureSelected;
}
});
},
value: selected == 'Country Dropdown' ? CountryVal : StateVal,
style: TextStyle(
color: Colors.blue,
fontSize: 18.0,
),
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_dropdown(_country, 'Country Dropdown'),
_dropdown(_state, 'State Dropdown')
],
),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
字符串CountryVal,StateVal;
列表_country=['-选择国家-'、'美国'、'英国'];
列表_state=['-选择state-'、'纽约'、'伦敦';
小部件下拉列表(列表选项,选定字符串){
返回容器(
填充:来自LTRB(15.0,4.0,15.0,4.0)的边缘设置,
边距:LTRB(20.0,7.0,20.0,10.0)的边距集,
子项:DropdownButtonHideUnderline(
孩子:下拉按钮(
是的,
提示:文本(
_选项[0],
样式:TextStyle(
颜色:颜色,蓝色,
字体大小:18.0,
),
),
项目:_options.map((字符串值){
返回下拉菜单项(
价值:价值,
子项:文本(值),
);
}).toList(),
onChanged:(已选择字符串捕获){
设置状态(){
如果(所选==“国家/地区下拉列表”){
CountryVal=captureSelected;
StateVal=_state[0];
}else if(已选==“状态下拉列表”){
StateVal=captureSelected;
}
});
},
值:selected==“国家/地区下拉列表”?CountryVal:StateVal,
样式:TextStyle(
颜色:颜色,蓝色,
字体大小:18.0,
),
),
),
);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
_下拉列表(_country,'country dropdown'),
_下拉列表(_state,“state dropdown”)
],
),
),
);
}
}