如何在Flutter的Table中为TableRow设置表格列大小

huangapple go评论84阅读模式
英文:

How to set table column size in TableRow for Flutter Table

问题

我不知道如何在Flutter中设置表格列的大小。如果我使用Row,可以使用Expanded,但TableRow不允许这样做。

请告诉我如何设置表格列大小的一些建议。
对我来说最好的解决方案是根据列中文本长度来调整大小。

英文:

I have no idea how to set table column size in Flutter. If I use Row, Expanded can be used, but TableRow does not allow it.

Please tell me some advice how to set table column size.
The best solution for me is adjust to size of text length in columns.

答案1

得分: 72

方法 1

如果您想要为每列设置相同的宽度,可以在Table中使用defaultColumnWidth

Table(
    defaultColumnWidth: FixedColumnWidth(200.0),
...

方法 2

如果您想为每列使用不同的宽度,可以这样做:

Table(
columnWidths: {
                0: FlexColumnWidth(1),
                1: FlexColumnWidth(4),
                2: FlexColumnWidth(4),
              },     
...
英文:

Method 1

You can use defaultColumnWidth inside Table if you want to set same width to each column,

Table(
    defaultColumnWidth: FixedColumnWidth(200.0),
...

Output

如何在Flutter的Table中为TableRow设置表格列大小


Method 2

you can use a different width for each column.

Table(
columnWidths: {
                0: FlexColumnWidth(1),
                1: FlexColumnWidth(4),
                2: FlexColumnWidth(4),
              },     
...

Output

如何在Flutter的Table中为TableRow设置表格列大小

答案2

得分: 23

对于您的特定情况,您可以使用以下代码:

Table(
  defaultColumnWidth: IntrinsicColumnWidth(),
  ...
);

因为您希望列宽根据文本长度自动调整。 IntrinsicColumnWidth 根据每个单元格中内容的宽度来确定列宽,即根据单元格中内容的长度增加或缩小列宽。

英文:

For your specific case you can use

Table(
  defaultColumnWidth: IntrinsicColumnWidth(),
  ...
);

, since you want the column width to adjust to the text lengths. IntrinsicColumnWidth sizes the column based on the width of the content in each cell, ie, the column width increases or shrinks depending upon the length of the content in the cell.

答案3

得分: 16

表格也可以使两列的宽度固定。

Table(
  border: TableBorder.all(color: Colors.black),
  columnWidths: {
    0: FixedColumnWidth(100.0),  // 固定宽度为100
    1: FlexColumnWidth(),
    2: FixedColumnWidth(100.0),  // 固定宽度为100
  },
)
英文:

You can also make two column widths fixed.

Table(
      border: TableBorder.all(color: Colors.black),
      columnWidths: {
            0: FixedColumnWidth(100.0),// fixed to 100 width
            1: FlexColumnWidth(),
            2: FixedColumnWidth(100.0),//fixed to 100 width
          },

huangapple
  • 本文由 发表于 2020年1月3日 13:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定