如何在 Flutter 中创建动态变量,我使用 ListView.builder 并说 itemCount 是 3(可以是动态的),因此要从下拉列表中存储值,我需要动态变量我有一个选项(可能是我没有) t 检查,因为它的复杂性)是制作一个 Map 然后制作一个类然后...... 我想让它更简单 所以我怎样才能让它变得更简单 (我认为可以使用密钥但我不知道如何使用它)所以请帮助
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');
});
}),
),
),
],
),
答案 0 :(得分:0)
我不确定我是否正确理解了您的意思,但是,如果您想将多个类格式作为 listView(或任何其他小部件)的输入,您应该使用父类(或接口)作为包装器!然后将其定义为该特定小部件的输入。现在,对于每个其他类,您可以扩展父类。因此,请按照以下步骤操作: 第一步:定义一个父类
class ParentClass {
}
第 2 步:通过构造函数将其定义为特定小部件的输入:
class MyWidget extends StatelessWidget {
ParentClass input;
MyWidget(this.input);
@override
Widget build(BuildContext context) {
return Container(color: const Color(0xFF2DBD3A));
}
}
class MyClass1 extends ParentClass{
// variables.
}