【发布时间】:2021-06-13 04:55:42
【问题描述】:
如何在 Flutter 中创建动态变量,我正在使用 ListView.builder 并说 itemCount 是 3(可以是动态的),因此要从下拉菜单中存储值,我需要动态变量我有一个选项(可能是我没有) t 检查,因为它的复杂性)是制作一个 Map 然后制作一个 Class 然后...... 我想让它更简单所以我怎样才能让它 (我觉得key可以用但是不知道怎么用)所以请帮忙
ListView.builder(
itemCount: shopCatList.length,
itemBuilder: (BuildContext context, int i) {
print('listview fr ${shopCatList[i]}');
tagItem = cate.cateProp[shopCatList[i]].tags;
brandItem = cate.cateProp[shopCatList[i]].brand;
// String k = shopCatList[i];
String _selectedTag;
String _selectedBrand;
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(shopCatList[i],
style: Theme.of(context)
.textTheme
.headline6
.copyWith(color: CustomColors.BlueDark)),
),
Container(
width: 150,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButton(
icon: Icon(Icons.arrow_drop_down),
isExpanded: true,
hint: Text(_selectedTag ?? 'Select Tags'),
value: _selectedTag,
items: tagItem.map((valueItem) {
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
onChanged: (newValue) {
setState(() {
_selectedTag = newValue;
print('onSelect $_selectedTag');
});
}),
),
),
],
),
【问题讨论】:
-
我不明白你所说的创建动态变量是什么意思
-
在上面的代码中,_selectedTag 是在 StateFul 小部件中定义的变量,其中 ListView.builder 是一个孩子现在这里的 ItemCount 是 3(比如说)所以当我选择一个下拉值的下拉值提示时,每个下拉值都会改变返回listview.builder的小部件平均值(对于所有itemCount),这给了我错误,所以我想要可变数量的_selectedTag(_selectedTag1,_selectedTag2,_selectedTag3),以便我可以输入_selectedTag [i] = newValue的值;
-
简单来说,我想将变量清除为 _selectedTag0 ,_selectedTag1 ... 像这个动态的变量数意味着整数值可以是 2、5、8 动态数,所以需要这么多变量被清除