我有一个工作正常的 DropdownMenu
,但我需要一个 ListView
,但我无法转换它。 DropdownMenu
如下所示:
// Create the List of devices to be shown in Dropdown Menu
List<DropdownMenuItem<BluetoothDevice>> _getDeviceItems() {
List<DropdownMenuItem<BluetoothDevice>> items = [];
if (_devicesList.isEmpty) {
items.add(
DropdownMenuItem(
child: Text(allTranslations.text(StringConstant.none)),
)
);
} else {
_devicesList.forEach((device) {
items.add(
DropdownMenuItem(
child: Text(device.name),
value: device,
));
});
}
return items;
}
我是这样调用它的
DropdownButton(
items: _getDeviceItems(),
onChanged: (value) => setState(() => _device = value),
value: _device,
),
我试着将它转换成这样的 listTile:
// Create the List of devices to be shown in Dropdown Menu
List<ListTile> _getDeviceListTiles() {
List<ListTile> items = [];
if (_devicesList.isEmpty) {
items.add(
ListTile(
title: Text(allTranslations.text(StringConstant.none)),
)
);
} else {
_devicesList.forEach((device) {
items.add(
ListTile(
title: Text(device.name),
));
});
}
return items;
}
如您所见,我设法将其转换为 ListTile
列表,但问题是我现在不知道如何在 ListView
中调用此列表。
最佳答案
您可以重构代码,也可以对现有代码使用这两种简单的方法。
- 简单的 ListView
ListView(
padding: const EdgeInsets.all(8.0),
children: _getDeviceListTiles()
);
- ListView.builder()
ListView.builder(
padding: const EdgeInsets.all(8.0),
itemCount: _getDeviceListTiles().length,
itemBuilder: (BuildContext context, int index) {
return _getDeviceListTiles()[index];
}
);
关于listview - 下拉 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57142377/