Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to display selected Dropdown values index number in flutter?

My DropDown working fine. I can Select item from my dropdown and also showing the selected item in my App. Now, Instead of displaying the selected item from dropdown I want to display index number of that particular Item.

Example image

My Dropdown item List

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    List<String> qualities = [
    "Creativity",
    "Commitment",
    "Planning",
    "Optimism",
    "Innovative"
  ];

Empty List. Here I’m storing selected items, then displaying using for loop

 List<String> selectedItem_ArrayList = []; //Storing DropDown selected data

My dropDown

DropdownButton<String>(
                          underline: Container(),
                          icon: Icon(Icons.keyboard_arrow_down),
                          iconSize: 24,
                          isExpanded: true,
                          onChanged: (value) {
                            if (!selectedItem_ArrayList.contains(value)) {//=============================================>>>Items Disable after selection
                              setState(() {
                                selectedQualities = value!;
                                selectedItem_ArrayList.add(selectedQualities);
                              });
                            }//==============================================>>>Adding item into list
                          },
                          value: selectedQualities,
                          items: qualities.map((dropdownValue) => DropdownMenuItem(
                                    child: Text(
                                      dropdownValue,
                                      maxLines: 2,
                                      style: TextStyle(
                                        color: selectedItem_ArrayList.contains(dropdownValue)? Colors.grey: null,
                                      ),
                                    ),
                                    value: dropdownValue,
                                  ))
                              .toList(),
                        ),

Displaying Selected Item from drop Dropdown

 Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  for (var i = 0; i < selectedItem_ArrayList.length; i++)
                    ListTile(
                      title: Text(selectedItem_ArrayList[i]), //### here I want to show index number of that particular selected item.
                      trailing: GestureDetector(//#################### remove specific index item
                        onTap: () {
                          setState(() {
                            selectedItem_ArrayList.remove(selectedItem_ArrayList[i]);
                          });
                        },
                        child: Icon(
                          Icons.delete_rounded,
                          color: Colors.red,
                        ),
                      ),
                    ),
                ],
              )

Using indexOf its only displaing index number 0 for every item selection

title: Text(selectedItem_ArrayList[i].indexOf(selectedItem_ArrayList[i]).toString()),

>Solution :

I get that you’re wanting to display the index number of the element in the original qualities list ?

The simplest way there is to either do a check on the original list, or to have a index table to quickly get the needed index.

In the first case, all you would do is call something like qualities.indexOf(selectedItem_ArrayList[i]), and it should do the trick.

If you want an index table, i would probably convert the qualities list to a Map<String, int> :

final qualities = <String, int>{
  "Creativity": 0,
  "Commitment": 1,
  "Planning": 2,
  "Optimism": 3,
  "Innovative": 4,
};

Then, instead of using qualities.map in the DropdownButton, you would use qualities.keys.map.
Once you need to get to the index back, you can just call qualities[selectedItem_ArrayList[i]).

As a quick note, your selectedItem_ArrayList[i].indexOf(selectedItem_ArrayList[i]) doesn’t do what you expect it to do, as you do indexOf on a specific element on your list, which means you do it on a String. Currently, it returns 0, as the Pattern you pass in argument of the indexOf is the string itself, so it validates that the Pattern is found from the beginning of the String. If it didn’t, it would return -1.

If you wanted to display the index of the element in the select items list, you would do selectedItem_ArrayList.indexOf(selectedItem_ArrayList[i]).

Hope it helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading