Search code examples
flutterflutter-layout

Flutter - Table / TableCell: How to set background color of one cell


I have a table widget and I have to adjust the background color of one cell. So I put a Container widget in the TableCell widget and set its color. This works fine if all cells of that row have the same height (b1). However if the height of another cell is larger (b2), my approach fails as you can see in this image.

enter image description here

I understand that the height of the Container matches its child and not the height of its parent. But I could not come up with a better approach. Any ideas?

Here is the code I used for the table:

Table(
    border: TableBorder.symmetric(
        inside: BorderSide(width: 1, color: AppColors.grey600),
        outside: BorderSide.none
    ),
    children: [
        TableRow(
            children: [
                TableCell(child: Container(child: Text('a'))),
                TableCell(child: Container(child: Text('b'))),
                TableCell(child: Container(child: Text('c'))),
            ],
        ),
        TableRow(
            children: [
                TableCell(child: Container(child: Text('abc abc abc'))),
                TableCell(child: Container(color: Colors.grey[300], child: Text('b'))),
                TableCell(child: Container(child: Text('c'))),
            ],
        ),
        TableRow(
            children: [
                TableCell(child: Container(child: Text('a'))),
                TableCell(child: Container(child: Text('b'))),
                TableCell(child: Container(child: Text('c'))),
            ],
        )
    ]
)

Solution

  • Use verticalAlignment: TableCellVerticalAlignment.fill

    TableCell(
      verticalAlignment: TableCellVerticalAlignment.fill, 
      child: Container(
        color: Colors.grey[300], 
        child: Text('b')
      )
    ),