skip to Main Content

I am trying to implement dynamic dropdownButton in my app where the items of dropdown is going to come from the names of columns in my excel sheet. I am able to show all the columns of excel but I couldn’t able to trace the index of column which the user is selecting from the dropdown.

I tried to make a map of dropdownitems like this in which the key is the index and value is the DropdownMenuItem like this :

late int selectedIndex;  //where I want to store the selected index
late String initialDropDownVal;
List<Map<int,DropdownMenuItem<String>>> dropdownItems = [];

Then I added some values by iterating the columns of excel using a for loop :

excel = Excel.decodeBytes(bytes);
sheet = excel['Sheet1'];
for(int i = 1; i< sheet.maxCols; ++i){
   var cell = sheet.cell(CellIndex.indexByColumnRow(rowIndex: 0, columnIndex: i));
   String val = cell.value.toString(); 
   if(val=="null"){
        break;
   }else{
      if(i==1){
         initialDropDownVal = val;
      }
      var newItem = DropdownMenuItem(
         child: Text(val),
              value: val,
          );
       dropdownItems.add({i:newItem});
   }

}

But I could not able to map the values in items attribute of DropdownButton, I tried to implement like this but this is throwing error

DropdownButton(
 value: selectedVal,
 icon: const Icon(Icons.keyboard_arrow_down),
 items: dropdownItems.map((int i,DropdownMenuItem<String> p) => p).toList(),
 onChanged: (String? value){
      setState(() {
            initialDropDownVal = value!;
       });
})                                        

And I am not sure how to change set the selectedIndex in onChanged function. Please help me in this. Thanks

2

Answers


  1. Chosen as BEST ANSWER
    late int selectedIndex;  //where I want to store the selected index
    late String initialDropDownVal;
    List<Map<String,int>> dropdownItems = [];
    
    excel = Excel.decodeBytes(bytes);
    sheet = excel['Sheet1'];
    for(int i = 1; i< sheet.maxCols; ++i){
       var cell = sheet.cell(CellIndex.indexByColumnRow(rowIndex: 0, columnIndex: i));
       String val = cell.value.toString(); 
       if(val=="null"){
            break;
       }else{
          if(i==1){
             initialDropDownVal = val;
          }
          dropdownItems.add({val:i}); 
       }
    
    }
    
    DropdownButton(
     value: selectedVal,
     icon: const Icon(Icons.keyboard_arrow_down),
     items: dropdownItems.map((e) {
                 return DropdownMenuItem(
                        child: Text(e.keys.first),
                        value: e.keys.first,
                 );
           }).toList(),
    onChanged: (String? value){
                setState(() {
                initialDropDownVal = value!;
                              
                for(int i = 0; i< dropdownItems.length; ++i){
                    if(dropdownItems[i].keys.first==value){
                         setState(() {
                               selectedIndex = dropdownItems[i].values.first;
                           
                          });
                          break;
                                }
                              }
                            });
                          }),
    )
    

  2. late int selectedIndex;  //where I want to store the selected index
    late String initialDropDownVal;
    List<DropdownMenuItem<String>> dropdownItems = [];
    
    
    excel = Excel.decodeBytes(bytes);
    sheet = excel['Sheet1'];
    for(int i = 1; i< sheet.maxCols; ++i){
       var cell = sheet.cell(CellIndex.indexByColumnRow(rowIndex: 0, columnIndex: i));
       String val = cell.value.toString(); 
       if(val=="null"){
            break;
       }else{
          if(i==1){
             initialDropDownVal = val;
          }
          var newItem = DropdownMenuItem(
             child: Text(val),
                  value: val,
              );
           dropdownItems.add(newItem);
       }
    
    }
    
    
    DropdownButton(
     value: selectedVal,
     icon: const Icon(Icons.keyboard_arrow_down),
     items: dropdownItems,
     onChanged: (String? value){
          setState(() {
                initialDropDownVal = value!;
    selectedIndex=dropdownItems.indexWhere((i)=>i.value==value);
           });
    }) 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search