listview - 下拉 ListView

标签 listview flutter drop-down-menu dart flutter-layout

我有一个工作正常的 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 中调用此列表。

最佳答案

您可以重构代码,也可以对现有代码使用这两种简单的方法。

  1. 简单的 ListView
ListView(
  padding: const EdgeInsets.all(8.0),
  children: _getDeviceListTiles()
);
  1. 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/

相关文章:

android - 如何在 android 中关闭搜索 View 后重置 ListView ?

javascript - 解析 JSON 并使用 jQuery mobile 中的数据扩展 ListView

java - Activity 销毁后自定义 ListView 不响应监听器

dart - Flutter ReorderableListView 示例

flutter - 有什么方法可以在 Flutter for Web 中添加谷歌地图?

flutter - 如何在2种颜色的 flutter 中创建Flatbutton?不渐变,并排2种纯色

jQuery UI Select 与验证插件结合使用

html - 编写一个水平下拉菜单,使链接相互重叠

jquery - 下拉菜单,如 http ://www. teslamotors.com/

android - 如何使用自定义适配器和 ListView 获取复选框值