Flutter 如何以编程方式清除颤振下拉按钮?
我有两个相互排斥的下拉按钮。当设置了一个值时,如何清除(或设置)另一个值Flutter 如何以编程方式清除颤振下拉按钮?,flutter,Flutter,我有两个相互排斥的下拉按钮。当设置了一个值时,如何清除(或设置)另一个值 谢谢当按下中的第一个下拉菜单时,然后尝试在onChanged事件中重置setState中的第二个下拉菜单的值,反之亦然 onChanged: (String newValue) { setState(() { dropdownValueFirst = newValue; dropdownValueSecond='Initial Value of second',//
谢谢当按下中的第一个下拉菜单时,然后尝试在
onChanged
事件中重置setState
中的第二个下拉菜单的值,反之亦然
onChanged: (String newValue) {
setState(() {
dropdownValueFirst = newValue;
dropdownValueSecond='Initial Value of second',// remeber this value must be same as initial value of 2nd dropdown =>value: 'Initial Value of second',
});
},
对于第一个下拉列表:
onChanged: (String newValue) {
setState(() {
dropdownValueFirst = newValue;
dropdownValueSecond = "Bangladesh";
});
},
onChanged: (String newValue) {
setState(() {
dropdownValueSecond = newValue;
dropdownValueFirst ="One";
});
},
对于第二个下拉列表:
onChanged: (String newValue) {
setState(() {
dropdownValueFirst = newValue;
dropdownValueSecond = "Bangladesh";
});
},
onChanged: (String newValue) {
setState(() {
dropdownValueSecond = newValue;
dropdownValueFirst ="One";
});
},
见以下代码:
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String dropdownValueFirst="One";
String dropdownValueSecond="Bangladesh";
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButton<String>(
value: dropdownValueFirst,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(
color: Colors.deepPurple
),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
setState(() {
dropdownValueFirst = newValue;
dropdownValueSecond = "Bangladesh";
});
},
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
})
.toList(),
),
const Padding(padding: EdgeInsets.only(left: 8)),
DropdownButton<String>(
value: dropdownValueSecond,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(
color: Colors.deepPurple
),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
setState(() {
dropdownValueSecond = newValue;
dropdownValueFirst ="One";
});
},
items: <String>['Bangladesh', 'India', 'China']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
})
.toList(),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:font_awesome_flatter/font_awesome_flatter.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
//此小部件是应用程序的主页。它是有状态的,表示
//它有一个状态对象(定义如下),其中包含影响
//看起来怎么样。
//此类是状态的配置。它保存值(在此
//案例名称)由家长(在本例中为应用程序小部件)提供,以及
//由State的build方法使用。小部件子类中的字段包括
//始终标记为“最终”。
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
字符串dropdownValueFirst=“一”;
字符串dropdownValueSecond=“孟加拉”;
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
下拉按钮(
值:dropdownValueFirst,
图标:图标(图标。向下箭头),
iconSize:24,
海拔:16,
样式:TextStyle(
颜色:颜色。深紫色
),
下划线:容器(
身高:2,
颜色:颜色。深紫色,
),
onChanged:(字符串newValue){
设置状态(){
dropdownValueFirst=新值;
dropdownValueSecond=“孟加拉国”;
});
},
物品:[“一”、“二”、“免费”、“四”]
.map((字符串值){
返回下拉菜单项(
价值:价值,
子项:文本(值),
);
})
.toList(),
),
常量填充(填充:仅限边集(左:8)),
下拉按钮(
值:dropdownValueSecond,
图标:图标(图标。向下箭头),
iconSize:24,
海拔:16,
样式:TextStyle(
颜色:颜色。深紫色
),
下划线:容器(
身高:2,
颜色:颜色。深紫色,
),
onChanged:(字符串newValue){
设置状态(){
dropdownValueSecond=新值;
dropdownValueFirst=“一”;
});
},
项目:[“孟加拉国”、“印度”、“中国”]
.map((字符串值){
返回下拉菜单项(
价值:价值,
子项:文本(值),
);
})
.toList(),
),
],
),
),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),//此尾随逗号使生成方法的自动格式设置更方便。
);
}
}
看看这个