gpt4 book ai didi

flutter - Flutter在ListView中添加项目

转载 作者:行者123 更新时间:2023-12-03 04:39:48 26 4
gpt4 key购买 nike

如何将项目从下拉列表添加到列表 View ?
这是可搜索下拉菜单的代码。我创建了一个添加按钮,我在理解如何从下拉列表中将所选项目直接添加到ListView时遇到了问题。目前,我只在Listview中返回一些文本。 (下拉菜单中每个元素的名称为Galati)

List<PlatformReach> _platformReach = PlatformReach.getPlatformReach();
List<DropdownMenuItem<PlatformReach>> _dropdownPlatformReach;

PlatformReach _selectedPlatformReach;

void initState() {
_dropdownMenuItems = buildDropDownMenuItems(_visibility);
_selectedVisibility = _dropdownMenuItems[0].value;

_dropdownPlatformReach =
buildDropdownMenuItemsPlatformReach(_platformReach);
_selectedPlatformReach = _dropdownPlatformReach[0].value;

super.initState();
}

List<DropdownMenuItem<PlatformReach>> buildDropdownMenuItemsPlatformReach(
List platforms) {
List<DropdownMenuItem<PlatformReach>> items = List();
for (PlatformReach platform in platforms) {
items.add(
DropdownMenuItem(
value: platform,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
platform.name,
style: TextStyle(fontWeight: FontWeight.bold),
textAlign: TextAlign.start,
),
Text(
platform.hint,
style: TextStyle(
fontWeight: FontWeight.normal, color: Colors.grey),
textAlign: TextAlign.end,
)
],
),
),
);
}
return items;
}
Expanded(

child: SearchableDropdown.single(
isExpanded: true,
value: _selectedPlatformReach,
hint: " ",
items: _dropdownPlatformReach,
onChanged: (PlatformReach selectedPlatformReach) {
setState(() {
_selectedPlatformReach = selectedPlatformReach;
});
},
),
flex: 2,
),
class PlatformReach {
String name;
String hint;

PlatformReach(this.name, this.hint);

static List<PlatformReach> getPlatformReach() {
return <PlatformReach>[
PlatformReach('Jud Galati', '(RO, [Galati] County)'),
PlatformReach('Jud Braila', '(RO, [Braila] County)'),
PlatformReach('Jud Prahova', '(RO, [Ploiesti] County)'),
PlatformReach('Jud Maramures', '(RO, [Baia Mare] County)'),
];
}
}
ListView.builder(
padding: EdgeInsets.only(left: 10),
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: _platformReach.length,
itemBuilder: (BuildContext ctxt, int Index) {
return new Text('Galati');
}),

最佳答案

首先应使用DropdownButton。那么您应该使用Listview对此进行包装。之后,您可以通过映射添加项目。下面是一个简单的示例。

ListView.builder(
itemCount: 20,
itemExtent: 50.0,
itemBuilder: (BuildContext context, int index) {
for (int i = 0; i < 20; i++) {
selectedItemValue.add("NONE");
}
return DropdownButton(
value: selectedItemValue[index].toString(),
items: _dropDownItem(),
onChanged: (value) {
selectedItemValue[index] = value;
setState(() {});
},
hint: Text('Select drowdown'),
);
}),
)
],
));



List<DropdownMenuItem<String>> _dropDownItem() {
List<String> ddl = ['Jud Galati', '(RO, [Galati] County)'];
return ddl
.map((value) => DropdownMenuItem(
value: value,
child: Text(value),
))
.toList();
}

关于flutter - Flutter在ListView中添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63446016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com