I've created a drop down menu in a stateful widget and this is working great!
@override
Widget build(BuildContext context) {
return DropdownButton(
dropdownColor: kBlueGrey900,
value: selectedFaction,
items: factions.map<DropdownMenuItem<String>>((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text(
item,
style: TextStyle(
color: kWhite,
fontWeight: FontWeight.bold,
fontSize: 20,
fontFamily: 'SourceSansPro',
),
),
);
}).toList(),
onChanged: (String? item) {
setState(() {
selectedFaction = item!;
});
},
),
what i don't understand is how to set the state of the app e.g. another widgets visibility depending on the users selection. So if they choose number 6 in the factions list, I then want a bool used for a widgets visibility to change to true.
thanks so much
CodePudding user response:
Two ways you can do this:
In case both the widgets (one with faction & one which you need to change the visibility) reside in same class, declare a parallel bool array of all falses at class level , the length of which will be same as faction.
In faction onChange method, set state of that index (say 6) to true.
Bind the second widget's visibility to the corresponding index in the bool array.
In case both widgets reside in different pages/files. Declare a common file with name for example
ApplicationParams.dart
. Do all of the above (as in point 1) by declaring the bool array as static. Access the array for exampleboolVisibilityArray
asApplicationParams.boolVisibilityArray