flutter DataTable 多行换行和居中

Wil*_*ill 6 datatable flutter

我试图在颤动中的 DataTable() 的 DataColumn() 行中有多个居中的行。不过,似乎不支持居中或多行。

我的数据表代码看起来像这样:

class TestDayData extends StatelessWidget {
  final List<String> timesList = [
    "This is",
    "a bunch",
    "of strings",
  ];

  final String day;

  TestDayData({Key key, this.day}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: DataTable(
        showCheckboxColumn: false,
        columns: [
          DataColumn(
            label: Center(child: Text(day)),
            numeric: false,
          ),
        ],
        rows: timesList
            .map(
              (times) => DataRow(cells: [
                DataCell(
                  Text(times.toString()),
                ),
              ]),
            )
            .toList(),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

我在这里制作了一个 dartpad 文件,以便在更大的上下文中显示上述代码。(我将多个 DataTable 放入 Row 小部件中,而不是在所有日子中使用一个 DataTable 的原因是因为我计划将它们中的每一个放入 Stack 小部件中,以便我可以将约会覆盖在列的顶部。 )

https://dartpad.dev/44bbb788e0d5f1e6393dd38a29430981

到目前为止,我可以通过添加空格并使用换行符来近似多行居中的 DataColumn 行,如 dartpad 文件中所示。(但必须有更好的方法!)

Nav*_*idi 7

您缺少小部件textAlign中的属性Text

DataTable(
  showCheckboxColumn: false,
  columns: [
    DataColumn(
      label: Center(child: Text(day, textAlign:TextAlign.center)),
        numeric: false,
      ),
    ],
    rows: timesList
      .map((times) => DataRow(cells: [
         DataCell(
           Text(times.toString(), textAlign: TextAlign.center),
         ),
      ]),
    )
    .toList(),
),
Run Code Online (Sandbox Code Playgroud)


Tin*_*825 6

对于我来说DataColumn,使用Center小部件或小部件textAlign上的属性Text不起作用:

这是我的解决方案:

DataColumn(
  label: Expanded(
    child: Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: const [Text("text")],
    ),
  ),
),
Run Code Online (Sandbox Code Playgroud)

DataCellCenter与小部件配合得很好