so i'm wanting to create an object in flutter that takes a Map<String, int> as a list of values, where the String is what is displayed in the menu, and the int is stored inside the class for later usage after an item is selected.
i am unsure how to do this. the part involving returning the int value eludes me.
the map structure is written like this:
final Map<String, int> discBasis = {
'Age': 1,
'Ancestry': 2,
'Color': 3,
'Disability': 4,
'Race': 5,
'Religion': 6,
'Sex': 7,
'Familial Status': 8,
'National Origin': 9
};
& the class which i am currently using:
final double formMarginHoriz = 10.0;
final double formMarginVert = 5.0;
class DropDownFromMap extends StatefulWidget {
Map<String, int> kv;
DropDownFromMap(this.kv);
@override
DropDownFromMapState createState() => new DropDownFromMapState(kv);
}
class DropDownFromMapState extends State<DropDownFromMap> {
Map<String, int> kv;
DropDownFromMapState(this.kv);
int selectedVal;
@override
Widget build(BuildContext context) {
return new Container(
margin: EdgeInsets.only(
top: formMarginVert,
bottom: formMarginVert,
left: formMarginVert,
right: formMarginHoriz),
child: new DropdownButton<int>(
hint: new Text("Select an option"),
value: selectedVal,
onChanged: (String newVal) {
setState(() {
selectedVal = kv[newVal];
});
},
items: kv.map((String k, _) {
return new DropdownMenuItem<String>(
value: k,
child: new Text(k),
);
}).toList(),
));
}
}
and it is instantiated like this (this is probably wrong):
@override
Widget build(BuildContext context) {
return new Material(
color: Colors.blueAccent,
child: new Container(
child: new Form(
key: _formKey,
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new DropDownFromMap(data.offenderType),
...
more context can be found at the github project: https://github.com/chscodeforchange/icrc-app in the relevant files: dropdown.dart, off_page_one.dart, & form_data.dart.
thank you in advance!