這是我的資料表:
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
我希望表格的高度受到限制,以便您可以滾動瀏覽它,但水平方向的寬度應該是固定的,因此您無法滾動。如果內容不適合列,如上述錯誤所示,則文本不應從螢屏上消失,而是文本應進入第二行,以便螢屏上的兩列都有足夠的空間。我已經嘗試了很多東西,但我根本無法限制表格的寬度,使其僅與父小部件一樣寬。
uj5u.com熱心網友回復:
為每個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, (確保最小化填充)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/420288.html
標籤:
