如何在 flutter 中使用 DropDownButton 作为动态列表?

How to use DropDownButton for dynamic list in flutter?

我正在尝试在我的应用程序中实现动态 dropdownButton,其中下拉项目将来自我的 excel sheet 中的列名称。我能够显示 excel 的所有列,但我无法跟踪用户从下拉列表中选择的列的索引。

我尝试制作一个像这样的下拉项映射,其中键是索引,值是 DropdownMenuItem,如下所示:

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

然后我通过使用 for 循环迭代 excel 的列来添加一些值:

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});
   }

}

但我无法映射 DropdownButton 的 items 属性中的值,我尝试这样实现,但这是抛出错误

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!;
       });
})                                        

而且我不确定如何在 onChanged 函数中更改设置 selectedIndex。请帮助我。谢谢

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);
       });
}) 
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;
                            }
                          }
                        });
                      }),
)