当其他下拉值在 flutter 中发生变化时,将下拉值重置为偏移量 0
Reset value of dropdown to offset 0 when other dropdown value changed in flutter
我正在使用下拉小部件来显示其中的多个下拉菜单。
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,
),
),
),
);
}
而且我已经多次针对不同的下拉菜单使用它
List<String> _country = ['- Select Country -', 'USA', 'UK'];
List<String> _state = ['- Select State -', 'New York', 'London'];
_dropdown(_country, 'Country Dropdown');
_dropdown(_state, 'State Dropdown');
现在的值'State dropdown'取决于'Country dropdown',我需要将'State dropdown'的值重置为偏移0时'Country dropdown'值。
我花了几个小时自己搜索和尝试,但我还是做对了。请帮忙
您可以复制粘贴 运行 下面的完整代码
选择国家/地区下拉值时,您可以设置 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')
],
),
),
);
}
}
我正在使用下拉小部件来显示其中的多个下拉菜单。
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,
),
),
),
);
}
而且我已经多次针对不同的下拉菜单使用它
List<String> _country = ['- Select Country -', 'USA', 'UK'];
List<String> _state = ['- Select State -', 'New York', 'London'];
_dropdown(_country, 'Country Dropdown');
_dropdown(_state, 'State Dropdown');
现在的值'State dropdown'取决于'Country dropdown',我需要将'State dropdown'的值重置为偏移0时'Country dropdown'值。
我花了几个小时自己搜索和尝试,但我还是做对了。请帮忙
您可以复制粘贴 运行 下面的完整代码
选择国家/地区下拉值时,您可以设置 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')
],
),
),
);
}
}