I want to pass selected value from DropDown to its parent and then to child through constructor, after that update list by setState functions.
So, I want selected value from Dropdown be accessible in child of Restrictions (UserOffers).
Sending data from parent to child its actually working, but only when im passing data definied in parent class to child by constructor = im not sure if its a good way.
Now i am using provider to update data on Dropdown only, but i dont know how to pass it.
CountryProvider class:
class CountryProvider with ChangeNotifier {
List<String> _items = [
"Argentina",
"Brazil",
...
"Spain",
"Switzerland",
];
String _selectedItem;
List<String> get items => _items;
String get selected => _selectedItem;
void setSelectedItem(String s) {
_selectedItem = s;
notifyListeners();
}
}
CountryDropDown class:
class CountryDropDown extends StatefulWidget {
@override
_CountryDropDownState createState() => _CountryDropDownState();
}
class _CountryDropDownState extends State<CountryDropDown> {
@override
void initState() {
super.initState();
}
...
ChangeNotifierProvider<CountryProvider>(
create: (context) => CountryProvider(),
child: Container(
width: 215,
child: Consumer<CountryProvider>(
builder: (_, provider, __) {
return DropdownButton<String>(
hint: Text("Not selected"),
icon: Icon(Icons.flight),
value: dropdownValue,
onChanged: (String newValue) {
provider.setSelectedItem(newValue);
dropdownValue = provider.selected;
print(newValue);
},
items: provider.items
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
...
And parent:
Restrictions class:
class Restrictions extends StatefulWidget {
@override
_RestrictionsState createState() => _RestrictionsState();
}
class _RestrictionsState extends State<Restrictions> {
@override
void initState() {
super.initState();
}
...
Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 20),
child: FieldDropDown(),
),
Container(
padding: EdgeInsets.only(top: 10),
margin: EdgeInsets.only(left: 20),
child: CountryDropDown(),
),
],
),
Container(
child: DateSelector(),
),
],
),
Container(
child: UserOffers(
country: selectedCountry,
field: selectedField,
),
...