I created a DropdownButton as a StatefulWidget. The class is called MyDropDown, with the corresponding state called MyDropDownState.
I created a reset function in the MyDropDownState class:
void reset(){
setState((){
_selection = null;
});
}
which will set the selection to null and set the state of the dropdown, effectively resetting the dropdown.
The core of the problem is I have to call this function when an IconButton on the AppBar is pressed. I have tried multiple ways and just can't get access to the state of the MyDropDown class I created.
This is the code of MyDropDown and it's State, simplified:
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;
});
},
);
}
}
In my main page, I create a new MyDropDown
final MyDropDown cityDropdown = new MyDropDown(cityLookup);
then this is the AppBar (inside a Scaffold) that holds the IconButton I want to press to reset the Dropdown.
appBar : new AppBar(
title: new Text('Filter Jobs'),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.refresh),
onPressed: () {
print('Reset dropdowns');
//this is where I would call reset() on cityDropdown's state, if I could figure out how to get to it :/
},
),
],
),