如何解决当其他下拉值在颤振中更改时,将下拉值重置为偏移量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,);
}
我已经多次使用它进行不同的下拉菜单
List<String> _country = ['- Select Country -','USA','UK'];
List<String> _state = ['- Select State -','New York','London'];
_dropdown(_country,'Country Dropdown');
_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,);
}
@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')
],);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。