我创建了一个DropdownButton作为StatefulWidget.该类称为MyDropDown,具有名为MyDropDownState的相应状态.
我在MyDropDownState类中创建了一个reset函数:
void reset(){
setState((){
_selection = null;
});
}
Run Code Online (Sandbox Code Playgroud)
这会将选择设置为null并设置下拉列表的状态,从而有效地重置下拉列表.
问题的核心是当按下AppBar上的IconButton时我必须调用此函数.我尝试了多种方法,只是无法访问我创建的MyDropDown类的状态.
这是MyDropDown的代码,它的状态,简化:
class MyDropDown extends StatefulWidget {
final Map<String, String> _itemMap;
MyDropDown(this._itemMap);
@override
MyDropDownState createState() => new MyDropDownState();
}
class MyDropDownState extends State<MyDropDown> {
String _selection;
void reset(){
setState((){
_selection = null;
});
}
@override
void initState() {
_selection = null;
super.initState();
}
@override
Widget build(BuildContext context) {
return new DropdownButton(
value: _selection,
//getDropItems builds dropdown items
items: getDropItems(widget._itemMap),
onChanged: (s) {
setState(() {
_selection = s;
});
}, …Run Code Online (Sandbox Code Playgroud)