Skip to content

Commit a120342

Browse files
authored
Fix flex methods for min and max column widths (#131724)
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.
1 parent f9e4567 commit a120342

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

packages/flutter/lib/src/rendering/table.dart

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,11 @@ class MaxColumnWidth extends TableColumnWidth {
263263
@override
264264
double? flex(Iterable<RenderBox> cells) {
265265
final double? aFlex = a.flex(cells);
266-
if (aFlex == null) {
267-
return b.flex(cells);
268-
}
269266
final double? bFlex = b.flex(cells);
270-
if (bFlex == null) {
271-
return null;
267+
if (aFlex == null) {
268+
return bFlex;
269+
} else if (bFlex == null) {
270+
return aFlex;
272271
}
273272
return math.max(aFlex, bFlex);
274273
}
@@ -316,12 +315,11 @@ class MinColumnWidth extends TableColumnWidth {
316315
@override
317316
double? flex(Iterable<RenderBox> cells) {
318317
final double? aFlex = a.flex(cells);
319-
if (aFlex == null) {
320-
return b.flex(cells);
321-
}
322318
final double? bFlex = b.flex(cells);
323-
if (bFlex == null) {
324-
return null;
319+
if (aFlex == null) {
320+
return bFlex;
321+
} else if (bFlex == null) {
322+
return aFlex;
325323
}
326324
return math.min(aFlex, bFlex);
327325
}

packages/flutter/test/rendering/table_test.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,37 @@ void main() {
277277
);
278278
});
279279

280+
test('MaxColumnWidth.flex returns the correct result', () {
281+
MaxColumnWidth columnWidth = const MaxColumnWidth(
282+
FixedColumnWidth(100), // returns null from .flex
283+
FlexColumnWidth(), // returns 1 from .flex
284+
);
285+
final double? flexValue = columnWidth.flex(<RenderBox>[]);
286+
expect(flexValue, 1.0);
287+
288+
// Swap a and b, check for same result.
289+
columnWidth = const MaxColumnWidth(
290+
FlexColumnWidth(), // returns 1 from .flex
291+
FixedColumnWidth(100), // returns null from .flex
292+
);
293+
// Same result.
294+
expect(columnWidth.flex(<RenderBox>[]), flexValue);
295+
});
296+
297+
test('MinColumnWidth.flex returns the correct result', () {
298+
MinColumnWidth columnWidth = const MinColumnWidth(
299+
FixedColumnWidth(100), // returns null from .flex
300+
FlexColumnWidth(), // returns 1 from .flex
301+
);
302+
final double? flexValue = columnWidth.flex(<RenderBox>[]);
303+
expect(flexValue, 1.0);
304+
305+
// Swap a and b, check for same result.
306+
columnWidth = const MinColumnWidth(
307+
FlexColumnWidth(), // returns 1 from .flex
308+
FixedColumnWidth(100), // returns null from .flex
309+
);
310+
// Same result.
311+
expect(columnWidth.flex(<RenderBox>[]), flexValue);
312+
});
280313
}

0 commit comments

Comments
 (0)