首页 > 解决方案 > 如何从 DataTable 小部件颤动中的地图元素列表中呈现数据?

问题描述

我创建了一个包含几列的 DataTable 小部件。我有一个地图元素列表,我想在 DataTable 的每一行中按顺序呈现这些元素。我在 DataTable 中有两列作为 ID 和 Name。我想显示行内的每个元素,即 ID 为 1 的元素显示在第一行,然后是第二行的下一个元素。如何进行渲染,以便我有类似循环的东西,它遍历整个列表并逐个渲染元素?

List<Map<String,dynamic>> myList = [
    {
      'id':1,
      'name':'Ahmad'
    },
    {
      'id':2,
      'name':'Usama'
    },
    {
      'id':3,
      'name':'Habib'
    }
  ];

DataTable 小部件是:

SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: DataTable(
                  columns: [
                    DataColumn(label: Text('ID')),
                    DataColumn(label: Text('Name')),
                  ],
                  rows: [
                    DataRow(
                      cells: [
                          // How to render here ?
                      ]
                    ),
                  ]
              ),
            ),
          ),

标签: flutterdart

解决方案


它为你工作。

DataTable(
  columnSpacing: 50,
  columns: const <DataColumn>[
    DataColumn(
      label: Text(
        'ID',
        style: TextStyle(
          fontStyle: FontStyle.italic,
        ),
      ),
    ),
    DataColumn(
      label: Text(
        'Name',
        style: TextStyle(
          fontStyle: FontStyle.italic,
        ),
      ),
    ),
  
  ],
  rows: myList
      .map(
        (e) => DataRow(
          cells: [
            DataCell(
              Text(
                e['id'],
                style: TextStyle(
                  fontStyle: FontStyle.italic,
                ),
              ),
            ),
            DataCell(
              Text(
                e['name'],
                style: TextStyle(
                  fontStyle: FontStyle.italic,
                ),
              ),
            ),
          ],
        ),
      )
      .toList(),
),

推荐阅读