Fix flex methods for min and max column widths (#131724)
Fixes https://github.com/flutter/flutter/issues/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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user