flutter - 如何从 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 ?
]
),
]
),
),
),
解决方案
它为你工作。
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(),
),
推荐阅读
- php - 在浏览器上单击退格导航时如何防止访问管理端口
- android - 为什么 Android Studio 不能识别函数?
- javascript - 如何从我的网站自动登录到不同的网站,然后从他们的 HTML 中获取 href 链接?
- javascript - 在javascript中从数组中查找并删除嵌套元素?
- r - R:read_csv 将数字条目读取为逻辑 - 解析 col_logical 而不是 col_double
- php - Lumen 使用 Graph API 和社交名流
- c++ - 从主函数调用变量到其他类&无法使用它?
- php - 使用 WP 查询循环遍历 WooCommerce 产品的元值
- python - Discord Python bot 删除某个频道中不是命令的消息
- javascript - 如何根据条件获取对象数组