Home > database >  how to change the state of the app when certain values are selected from a drop down menu?
how to change the state of the app when certain values are selected from a drop down menu?

Time:11-19

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:

  1. 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.

  2. 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 example boolVisibilityArray as ApplicationParams.boolVisibilityArray

  • Related