简体   繁体   English

flutter动态列表如何使用DropDownButton?

[英]How to use DropDownButton for dynamic list in flutter?

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.我正在尝试在我的应用程序中实现动态 dropdownButton,其中下拉项目将来自我的 excel 工作表中的列名。 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.我能够显示 excel 的所有列,但我无法跟踪用户从下拉列表中选择的列的索引。

I tried to make a map of dropdownitems like this in which the key is the index and value is the DropdownMenuItem like this:我试图制作一个 map 这样的下拉项,其中键是索引,值是 DropdownMenuItem,如下所示:

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:然后我通过使用 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});
   }

}

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但是我无法 map 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!;
       });
})                                        

And I am not sure how to change set the selectedIndex in onChanged function. Please help me in this.而且我不确定如何更改onChanged function 中的selectedIndex集。请帮助我。 Thanks谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:如何使用 DropDownButton? - Flutter: how to use DropDownButton? 如何在 Flutter 中制作动态下拉按钮列表? (删除项目) - How to make a dynamic dropdownbutton list in Flutter ? (delete items) Flutter中DropdownButton中的TextEditingController如何使用? - How to use TextEditingController in DropdownButton in Flutter? 数据表中的 Flutter DropdownButton,列表中的 DropdownButton 选项 - Flutter DropdownButton in DataTable, DropdownButton options from list 如何解决 flutter dropdownButtonFormField 动态选择检查 dropdownButton 的值 - How to resolve flutter dropdownButtonFormField dynamic selection checking for dropdownButton's value 如何在 flutter DropDownButton 中设置提示动态? - How do I set hint dynamic in flutter DropDownButton? 如何在 Flutter 中显示 DropDownButton? - How to show DropDownButton in Flutter? 如何使用颤振中的列表中的 JSON 数据列表创建 DropdownButton - How to Create DropdownButton with a list of JSON data within a list in flutter 如何使用 JSON 数据列表创建 DropdownButton,我希望它在 Flutter 中填充我的 DropDownButton - How to Create DropdownButton with a list of JSON Data and I want it to populate my DropDownButton in Flutter 在 Flutter 中为 DropdownButton 创建项目列表 - Create Item list for DropdownButton in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM