首页 > 解决方案 > 向 TableRow 添加填充

问题描述

我正在尝试向我的表格行添加填充。

这就是我所拥有的:

var list = (report['items'] as List)
          .map((item) => 
          TableRow(children: [
                Text(item['place']),
                Text(item['type']),
                Text(item['producer']),
                Text(item['serial_number']),
                Text(formatter.format(DateTime.parse(item['next_check_date']))
                    .toString()),
                Text(item['test_result']),
                Text(item['comments']),
              ]))
          .toList();

这就是我试图做的:

var list = (report['items'] as List)
          .map((item) =>
      Container(
          padding: EdgeInsets.only(10.0),
          child: TableRow(children: [
                Text(item['place']),
                Text(item['type']),
                Text(item['producer']),
                Text(item['serial_number']),
                Text(formatter.format(DateTime.parse(item['next_check_date']))
                    .toString()),
                Text(item['test_result']),
                Text(item['comments']),
              ])))
          .toList();

但是我收到了这个错误(在添加了带有填充的容器之后):

The argument type 'TableRow' can't be assigned to the parameter type 'Widget'.

如何向我的 Table / TableRows 添加填充?

标签: flutterdartflutter-layout

解决方案


我发现在这种情况下工作(当您想在表格中的行之间添加填充时)是用TextWidget包装Container并将padding属性添加到您的元素之一(这可能是“最高”的元素)并将alignment属性用于其他容器,其中您需要在行内对齐文本。我的示例将如下所示(抱歉图像模糊):

Padding(
  padding: const EdgeInsets.fromLTRB(32.0, 0.0, 32.0, 0.0),
  child: Table(
    columnWidths: {
      0: FractionColumnWidth(.75),
    },
    children: achievementList
        .map(
          (achivement) => TableRow(
            children: [
              Container(
                padding: EdgeInsets.only(bottom: 10.0),
                child: Text(
                  achivement.title,
                  style: TextStyle(
                    fontSize: 16.0,
                  ),
                ),
              ),
              Container(
                alignment: Alignment.centerRight,
                child: Text(
                  achivement.dateString,
                  style: TextStyle(
                    fontSize: 16.0,
                  ),
                ),
              ),
            ],
          ),
        )
        .toList(),
  ),
),

achievementList我的对象在哪里titledateStringText通过首先用Widget包装Container并赋予第一个Container 属性padding: EgeInsets.only(bottom: 10.0),,它会为整行提供填充。

项目之间有填充的列表


推荐阅读