【问题标题】:Dropdown to listView下拉列表视图
【发布时间】:2019-11-30 05:08:51
【问题描述】:

我有一个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 flutter drop-down-menu dart flutter-layout


    【解决方案1】:

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

    1. 简单的列表视图
    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];
      }
    );
    

    【讨论】:

      猜你喜欢
      • 2014-09-22
      • 2015-07-31
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多