diff --git a/packages/flutter/lib/src/rendering/table.dart b/packages/flutter/lib/src/rendering/table.dart index 85d936023dd6..702504b6a71d 100644 --- a/packages/flutter/lib/src/rendering/table.dart +++ b/packages/flutter/lib/src/rendering/table.dart @@ -347,7 +347,10 @@ enum TableCellVerticalAlignment { /// Cells with this alignment are sized to be as tall as the row, then made to fit the row. /// If all the cells have this alignment, then the row will have zero height. - fill + fill, + + /// Cells with this alignment are sized to be the same height as the tallest cell in the row. + intrinsicHeight } /// A table where the columns and rows are sized to fit the contents of the cells. @@ -1048,6 +1051,7 @@ class RenderTable extends RenderBox { case TableCellVerticalAlignment.top: case TableCellVerticalAlignment.middle: case TableCellVerticalAlignment.bottom: + case TableCellVerticalAlignment.intrinsicHeight: final Size childSize = child.getDryLayout(BoxConstraints.tightFor(width: widths[x])); rowHeight = math.max(rowHeight, childSize.height); case TableCellVerticalAlignment.fill: @@ -1126,6 +1130,7 @@ class RenderTable extends RenderBox { case TableCellVerticalAlignment.top: case TableCellVerticalAlignment.middle: case TableCellVerticalAlignment.bottom: + case TableCellVerticalAlignment.intrinsicHeight: child.layout(BoxConstraints.tightFor(width: widths[x]), parentUsesSize: true); rowHeight = math.max(rowHeight, child.size.height); case TableCellVerticalAlignment.fill: @@ -1154,6 +1159,7 @@ class RenderTable extends RenderBox { case TableCellVerticalAlignment.bottom: childParentData.offset = Offset(positions[x], rowTop + rowHeight - child.size.height); case TableCellVerticalAlignment.fill: + case TableCellVerticalAlignment.intrinsicHeight: child.layout(BoxConstraints.tightFor(width: widths[x], height: rowHeight)); childParentData.offset = Offset(positions[x], rowTop); } diff --git a/packages/flutter/test/rendering/table_test.dart b/packages/flutter/test/rendering/table_test.dart index 521e695ada9e..ccda83041053 100644 --- a/packages/flutter/test/rendering/table_test.dart +++ b/packages/flutter/test/rendering/table_test.dart @@ -309,4 +309,33 @@ void main() { // Same result. expect(columnWidth.flex([]), flexValue); }); + + test('TableRows with differents constraints, but vertically with intrisicHeight', () { + const BoxConstraints firstConstraints = BoxConstraints.tightFor(width: 100, height: 100); + const BoxConstraints secondConstraints = BoxConstraints.tightFor(width: 200, height: 200); + + final RenderTable table = RenderTable( + textDirection: TextDirection.rtl, + defaultVerticalAlignment: TableCellVerticalAlignment.intrinsicHeight, + children: >[ + [ + RenderConstrainedBox(additionalConstraints: firstConstraints), + RenderConstrainedBox(additionalConstraints: secondConstraints), + ] + ], + columnWidths: const { + 0: FlexColumnWidth(), + 1: FlexColumnWidth(), + }, + ); + + const Size size = Size(300.0, 300.0); + + // Layout the table with a fixed size. + layout(table, constraints: BoxConstraints.tight(size)); + + // Make sure the table has a size and that the children are filled vertically to the highest cell. + expect(table.size, equals(size)); + expect(table.defaultVerticalAlignment, TableCellVerticalAlignment.intrinsicHeight); + }); } diff --git a/packages/flutter/test/widgets/table_test.dart b/packages/flutter/test/widgets/table_test.dart index b5531277d3ee..d09aaa915b15 100644 --- a/packages/flutter/test/widgets/table_test.dart +++ b/packages/flutter/test/widgets/table_test.dart @@ -1080,5 +1080,47 @@ void main() { ); }); + testWidgets('Set defaultVerticalAlignment to intrisic height and check their heights', (WidgetTester tester) async { + final Widget table = Directionality( + textDirection: TextDirection.ltr, + child: Table( + defaultVerticalAlignment: TableCellVerticalAlignment.intrinsicHeight, + children: const [ + TableRow( + children: [ + SizedBox(height: 100, child: Text('A')), + SizedBox(height: 200, child: Text('B')), + ], + ), + TableRow( + children: [ + SizedBox(height: 200, child: Text('C')), + SizedBox(height: 300, child: Text('D')), + ], + ), + ], + ), + ); + + // load and check if render object was created. + await tester.pumpWidget(table); + expect(find.byWidget(table), findsOneWidget); + + final RenderBox boxA = tester.renderObject(find.text('A')); + final RenderBox boxB = tester.renderObject(find.text('B')); + + // boxA and boxB must be the same height, even though boxB is higher than boxA initially. + expect(boxA.size.height, equals(boxB.size.height)); + + final RenderBox boxC = tester.renderObject(find.text('C')); + final RenderBox boxD = tester.renderObject(find.text('D')); + + // boxC and boxD must be the same height, even though boxD is higher than boxC initially. + expect(boxC.size.height, equals(boxD.size.height)); + + // boxD (300.0h) should be higher than boxA (200.0h) which has the same height of boxB. + expect(boxD.size.height, greaterThan(boxA.size.height)); + }); + // TODO(ianh): Test handling of TableCell object }