颤振数据表限制宽度到屏幕宽度和换行文本



这是我的数据表:

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

我想限制表格的高度,这样你就可以滚动它,但水平方向的宽度应该是固定的,这样你不能滚动。如果内容不适合列,如上面的错误所示,那么文本不应该从屏幕上消失,而是应该进入第二行,以便在屏幕上有足够的空间容纳两列。我已经尝试了很多方法,但我无法限制表的宽度,使其仅与父窗口小部件一样宽。

为每个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,并使用它的width: constraints.maxWidth * .5,来获得50%的屏幕宽度或MediaQuery,(确保最小化填充(。

FittedBox包装DataTable小部件解决了所有问题!

最新更新