Skip to content

Commit

Permalink
Fix flex methods for min and max column widths (#131724)
Browse files Browse the repository at this point in the history
Fixes flutter/flutter#131467

An error in the flex methods of min and max column width would produce different results based on the position of the widths that were provided:
`MaxColumnWidth(a, b) != MaxColumnWidth(b, a)`

This fixes that.
  • Loading branch information
Piinks authored Aug 2, 2023
1 parent f9e4567 commit a120342
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
18 changes: 8 additions & 10 deletions packages/flutter/lib/src/rendering/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,11 @@ class MaxColumnWidth extends TableColumnWidth {
@override
double? flex(Iterable<RenderBox> cells) {
final double? aFlex = a.flex(cells);
if (aFlex == null) {
return b.flex(cells);
}
final double? bFlex = b.flex(cells);
if (bFlex == null) {
return null;
if (aFlex == null) {
return bFlex;
} else if (bFlex == null) {
return aFlex;
}
return math.max(aFlex, bFlex);
}
Expand Down Expand Up @@ -316,12 +315,11 @@ class MinColumnWidth extends TableColumnWidth {
@override
double? flex(Iterable<RenderBox> cells) {
final double? aFlex = a.flex(cells);
if (aFlex == null) {
return b.flex(cells);
}
final double? bFlex = b.flex(cells);
if (bFlex == null) {
return null;
if (aFlex == null) {
return bFlex;
} else if (bFlex == null) {
return aFlex;
}
return math.min(aFlex, bFlex);
}
Expand Down
33 changes: 33 additions & 0 deletions packages/flutter/test/rendering/table_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,37 @@ void main() {
);
});

test('MaxColumnWidth.flex returns the correct result', () {
MaxColumnWidth columnWidth = const MaxColumnWidth(
FixedColumnWidth(100), // returns null from .flex
FlexColumnWidth(), // returns 1 from .flex
);
final double? flexValue = columnWidth.flex(<RenderBox>[]);
expect(flexValue, 1.0);

// Swap a and b, check for same result.
columnWidth = const MaxColumnWidth(
FlexColumnWidth(), // returns 1 from .flex
FixedColumnWidth(100), // returns null from .flex
);
// Same result.
expect(columnWidth.flex(<RenderBox>[]), flexValue);
});

test('MinColumnWidth.flex returns the correct result', () {
MinColumnWidth columnWidth = const MinColumnWidth(
FixedColumnWidth(100), // returns null from .flex
FlexColumnWidth(), // returns 1 from .flex
);
final double? flexValue = columnWidth.flex(<RenderBox>[]);
expect(flexValue, 1.0);

// Swap a and b, check for same result.
columnWidth = const MinColumnWidth(
FlexColumnWidth(), // returns 1 from .flex
FixedColumnWidth(100), // returns null from .flex
);
// Same result.
expect(columnWidth.flex(<RenderBox>[]), flexValue);
});
}

0 comments on commit a120342

Please sign in to comment.