diff --git a/packages/flutter/lib/src/rendering/flex.dart b/packages/flutter/lib/src/rendering/flex.dart index b472b2493f..61277ddb4b 100644 --- a/packages/flutter/lib/src/rendering/flex.dart +++ b/packages/flutter/lib/src/rendering/flex.dart @@ -595,18 +595,6 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin 0) { - final double flexFraction = childSize(child, extent) / _getFlex(child); + final double flexFraction = childSize(child, extent) / flex; maxFlexFractionSoFar = math.max(maxFlexFractionSoFar, flexFraction); } else { inflexibleSpace += childSize(child, extent); } - final FlexParentData childParentData = child.parentData! as FlexParentData; - child = childParentData.nextSibling; } return maxFlexFractionSoFar * totalFlex + inflexibleSpace; } else { @@ -633,50 +618,27 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin true, + Axis.vertical => false, + }; - // Get inflexible space using the max intrinsic dimensions of fixed children in the main direction. - final double availableMainSpace = extent; - int totalFlex = 0; - double inflexibleSpace = 0.0; - double maxCrossSize = 0.0; - RenderBox? child = firstChild; - while (child != null) { - final int flex = _getFlex(child); - totalFlex += flex; - late final double mainSize; - late final double crossSize; - if (flex == 0) { - switch (_direction) { - case Axis.horizontal: - mainSize = child.getMaxIntrinsicWidth(double.infinity); - crossSize = childSize(child, mainSize); - case Axis.vertical: - mainSize = child.getMaxIntrinsicHeight(double.infinity); - crossSize = childSize(child, mainSize); - } - inflexibleSpace += mainSize; - maxCrossSize = math.max(maxCrossSize, crossSize); - } - final FlexParentData childParentData = child.parentData! as FlexParentData; - child = childParentData.nextSibling; + Size layoutChild(RenderBox child, BoxConstraints constraints) { + final double mainAxisSizeFromConstraints = isHorizontal ? constraints.maxWidth : constraints.maxHeight; + // A infinite mainAxisSizeFromConstraints means this child is flexible (or extent is double.infinity). + assert((_getFlex(child) != 0 && extent.isFinite) == mainAxisSizeFromConstraints.isFinite); + final double maxMainAxisSize = mainAxisSizeFromConstraints.isFinite + ? mainAxisSizeFromConstraints + : (isHorizontal ? child.getMaxIntrinsicWidth(double.infinity) : child.getMaxIntrinsicHeight(double.infinity)); + return isHorizontal + ? Size(maxMainAxisSize, childSize(child, maxMainAxisSize)) + : Size(childSize(child, maxMainAxisSize), maxMainAxisSize); } - - // Determine the spacePerFlex by allocating the remaining available space. - // When you're overconstrained spacePerFlex can be negative. - final double spacePerFlex = math.max(0.0, (availableMainSpace - inflexibleSpace) / totalFlex); - - // Size remaining (flexible) items, find the maximum cross size. - child = firstChild; - while (child != null) { - final int flex = _getFlex(child); - if (flex > 0) { - maxCrossSize = math.max(maxCrossSize, childSize(child, spacePerFlex * flex)); - } - final FlexParentData childParentData = child.parentData! as FlexParentData; - child = childParentData.nextSibling; - } - - return maxCrossSize; + return _computeSizes( + constraints: isHorizontal ? BoxConstraints(maxWidth: extent) : BoxConstraints(maxHeight: extent), + layoutChild: layoutChild, + getBaseline: ChildLayoutHelper.getDryBaseline, + ).axisSize.crossAxisExtent; } } diff --git a/packages/flutter/test/rendering/flex_test.dart b/packages/flutter/test/rendering/flex_test.dart index e64ab986d9..0cb606dc35 100644 --- a/packages/flutter/test/rendering/flex_test.dart +++ b/packages/flutter/test/rendering/flex_test.dart @@ -773,11 +773,44 @@ void main() { expect(flex.getDryLayout(BoxConstraints.loose(size)), const Size(200.0, 30.0)); expect(flex.getDryBaseline(BoxConstraints.loose(size), TextBaseline.alphabetic), 20.0); size = const Size(300, 100); - // box 1 one line, box 2 one. + // box 1 one line, box 2 one line. expect(flex.getDryLayout(BoxConstraints.loose(size)), const Size(300.0, 20.0)); expect(flex.getDryBaseline(BoxConstraints.loose(size), TextBaseline.alphabetic), 10.0); }); + test('baseline aligned children cross intrinsic size', () { + // box1 has its baseline placed at the top of the box. + final RenderFlowBaselineTestBox box1 = RenderFlowBaselineTestBox() + ..baselinePlacer = ((double height) => 0.0) + ..gridCount = 10; + + // box2 has its baseline placed at the bottom of the box. + final RenderFlowBaselineTestBox box2 = RenderFlowBaselineTestBox() + ..baselinePlacer = ((double height) => height) + ..gridCount = 10; + + final RenderFlex flex = RenderFlex( + textDirection: TextDirection.ltr, + textBaseline: TextBaseline.alphabetic, + crossAxisAlignment: CrossAxisAlignment.baseline, + children: [box1, box2], + ); + final FlexParentData box1ParentData = box1.parentData! as FlexParentData; + box1ParentData.flex = 2; + box1ParentData.fit = FlexFit.tight; + final FlexParentData box2ParentData = box2.parentData! as FlexParentData; + box2ParentData.flex = 1; + box2ParentData.fit = FlexFit.loose; + + // box 1 one line, box 2 two lines. + expect(flex.getMaxIntrinsicHeight(200), 30); + expect(flex.getMinIntrinsicHeight(200), 30); + + // box 1 one line, box 2 one line. + expect(flex.getMaxIntrinsicHeight(300), 20); + expect(flex.getMinIntrinsicHeight(300), 20); + }); + test('children with no baselines do not affect the baseline location', () { // box1 has its baseline placed at the bottom of the box. final RenderFlowBaselineTestBox box1 = RenderFlowBaselineTestBox()