From a120342922924f9610a2fbf86a7e2f432e3ad1eb Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Wed, 2 Aug 2023 12:00:48 -0500 Subject: [PATCH] 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. --- packages/flutter/lib/src/rendering/table.dart | 18 +++++----- .../flutter/test/rendering/table_test.dart | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/src/rendering/table.dart b/packages/flutter/lib/src/rendering/table.dart index 2208664399..2cc86091ce 100644 --- a/packages/flutter/lib/src/rendering/table.dart +++ b/packages/flutter/lib/src/rendering/table.dart @@ -263,12 +263,11 @@ class MaxColumnWidth extends TableColumnWidth { @override double? flex(Iterable 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 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); } diff --git a/packages/flutter/test/rendering/table_test.dart b/packages/flutter/test/rendering/table_test.dart index 12ff18b035..78386e7c26 100644 --- a/packages/flutter/test/rendering/table_test.dart +++ b/packages/flutter/test/rendering/table_test.dart @@ -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([]); + 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([]), 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([]); + 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([]), flexValue); + }); }