【问题标题】:flutter DataTable multiline wrapping and centering颤振DataTable多行换行和居中
【发布时间】:2020-06-23 22:18:40
【问题描述】:

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

我的 DataTable 代码如下所示:

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(),
      ),
    );
  }
}

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

https://dartpad.dev/44bbb788e0d5f1e6393dd38a29430981

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

【问题讨论】:

    标签: flutter datatable


    【解决方案1】:

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

    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(),
          ),
    

    【讨论】:

      【解决方案2】:

      您可以尝试这样做以使您的文本在 Datacolumn 中居中

      DataColumn(
      label: Center( widthFactor: 1.4,
      child: Text("HELLO", textAlign: TextAlign.center,
              style: TextStyle(fontSize: 18.0,),),)),
      

      您可以尝试使用此方法将 Datacell 中的文本居中设置为行。

      DataCell( Center(child: Text("Hello")))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        • 1970-01-01
        • 1970-01-01
        • 2020-09-27
        • 2020-04-13
        • 2021-10-04
        • 2022-08-02
        相关资源
        最近更新 更多