• notice
  • Congratulations on the launch of the Sought Tech site

Flutter value in DropDownMenu

I was wondering if it's possible that you're using DropDownMenu in Flutter to get different values.

Example: You have items['item1','item2','item3','item4' ,'item5'] Then you use a DropDownButton for these items. but for each project a different value should be passed so I can use it later.

So item1=90, item2=180, item3=270 and so on.If item1 is selected, it should be displayed to the user like this, and the app gets the value 90, e.g.it can be used later.

I hope you understand what I mean and can help me a little bit.

uj5u.com enthusiastic netizens replied:

You can create a map and use it as the keyDropDownMenu, also displayed for key display values ​​like

 String? dropdownValue;

  Map<String, int> dataset={
    "item1": 90,
    "item2": 180,
    "item3": 270,
  Widget build(BuildContext context) {
    return Scaffold(
          child: Column(
        children: [
            child: DropdownButton<String>(
              value: dropdownValue,
              focusColor: Colors.white,
              items: dataset.keys.map((String value) {
                return DropdownMenuItem(
                  value: value,
                  child: Text(value),
              onChanged: (v) {
                setState(() {
                ? "Select Item "
                : dataset[dropdownValue!].toString(),

uj5u.com enthusiastic netizens replied:

There are many ways to do this.

1-by key-value pair.

Get your sample, eg:

items[{'name':'item1','value':'90' span>},{'name':'item2','value':'180'}, {'name':'item3','value':'270'},{ 'name':'item4','value':'360'}];

(probably they are 360 ​​degree angels :-))

Then you can point to each project like this: items[i]['name']oritems[j]['value']etc...

2-Not the preferred way: two different strings, like this:

itemNames[items1,item2,item3, item4];
itemValues ​​[90,180,270,360];

You can then pass the itemNames list to your dropdownMenu, as soon as an item is selected, you It can find its index and point to the corresponding row of itemsValues.Example: dropdownMenu item3 is selected, so the index passed back should be 2 (index starts at 0), so you can find the desired value like this: desiredValue=itemsValue[2];(270)


Technical otaku

Sought technology together

Related Topic


Leave a Reply