2

这是我的数据表:

SizedBox(
  height: 200,
  child: SingleChildScrollView(
    scrollDirection: Axis.vertical,
    child: DataTable(
      columnSpacing: 0,
      columns: const [
        DataColumn(label: Text("Domain")),
        DataColumn(label: Text("Request count")),
      ],
      rows: const [
        DataRow(cells: [
          DataCell(Text("A very long text which causes the right column to be pushed out of the screen")),
          DataCell(Text("12345")),
        ]),
        DataRow(cells: [
          DataCell(Text("It would be the best if this text would be shown in to lines so that there is enough space for the second column")),
          DataCell(Text("678890")),
        ]),
      ],
    ),
  ),
)

例外是:

The following assertion was thrown during layout:
A RenderFlex overflowed by 39 pixels on the right.

The relevant error-causing widget was
DataTable

我希望表格的高度受到限制,以便您可以滚动浏览它,但水平方向的宽度应该是固定的,因此您无法滚动。如果内容不适合列,如上述错误所示,则文本不应从屏幕上消失,而是应进入第二行,以便屏幕上的两列都有足够的空间。我已经尝试了很多东西,但我根本无法限制表格的宽度,使其仅与父小部件一样宽。

4

1 回答 1

1

为每个DataCell孩子提供宽度解决了这个问题。

DataCell(
  SizedBox(
    width: x,
    child: Text(
      "A very long text which causes the right column to be pushed out of the screen",
      overflow: TextOverflow.visible,
      softWrap: true,
    ),
  ),
),

要获得响应宽度,您可以LayoutBuilder在 body 上使用并使用它width: constraints.maxWidth * .5,来获得 50% 的屏幕宽度或MediaQuery, (确保最小化填充)。

于 2022-01-23T16:16:59.720 回答