Fix the RenderFlex.computeDryBaseline implementation to match computeDistanceToActualBaseline (#149062)
Per Hixie's comment [here](https://github.com/flutter/flutter/issues/145739#issuecomment-2128006146), keep the current behavior
diff --git a/packages/flutter/lib/src/rendering/flex.dart b/packages/flutter/lib/src/rendering/flex.dart
index 4726bd4..45de3a5 100644
--- a/packages/flutter/lib/src/rendering/flex.dart
+++ b/packages/flutter/lib/src/rendering/flex.dart
@@ -308,6 +308,8 @@
/// See also:
///
/// * [RenderBox.getDistanceToBaseline], which defines the baseline of a box.
+ /// * [IgnoreBaseline], which can be used to ignore a child for the purpose of
+ /// baseline alignment.
baseline;
double _getChildCrossAxisOffset(double freeSpace, bool flipped) {
@@ -821,13 +823,17 @@
final double freeSpace = math.max(0.0, sizes.mainAxisFreeSpace);
final bool flipMainAxis = _flipMainAxis;
final (double leadingSpaceY, double spaceBetween) = mainAxisAlignment._distributeSpace(freeSpace, childCount, flipMainAxis);
- double y = leadingSpaceY;
- final (_NextChild nextChild, RenderBox? topLeftChild) = flipMainAxis ? (childBefore, lastChild) : (childAfter, firstChild);
- for (RenderBox? child = topLeftChild; child != null; child = nextChild(child)) {
+ double y = flipMainAxis
+ ? leadingSpaceY + (childCount - 1) * spaceBetween + (sizes.axisSize.mainAxisExtent - sizes.mainAxisFreeSpace)
+ : leadingSpaceY;
+ final double directionUnit = flipMainAxis ? -1.0 : 1.0;
+ for (RenderBox? child = firstChild; baselineOffset == BaselineOffset.noBaseline && child != null; child = childAfter(child)) {
final BoxConstraints childConstraints = constraintsForChild(child);
final Size childSize = child.getDryLayout(childConstraints);
- baselineOffset = baselineOffset.minOf(BaselineOffset(child.getDryBaseline(childConstraints, baseline)) + y);
- y += spaceBetween + childSize.height;
+ final double? childBaselineOffset = child.getDryBaseline(childConstraints, baseline);
+ final double additionalY = flipMainAxis ? - childSize.height : 0.0;
+ baselineOffset = BaselineOffset(childBaselineOffset) + y + additionalY;
+ y += directionUnit * (spaceBetween + childSize.height);
}
case Axis.horizontal:
final bool flipCrossAxis = _flipCrossAxis;
diff --git a/packages/flutter/test/rendering/flex_test.dart b/packages/flutter/test/rendering/flex_test.dart
index 9dd722d..702761d 100644
--- a/packages/flutter/test/rendering/flex_test.dart
+++ b/packages/flutter/test/rendering/flex_test.dart
@@ -648,6 +648,53 @@
expect(box2.localToGlobal(Offset.zero).dy, 0.0);
});
+ test('Vertical Flex Baseline', () {
+ const BoxConstraints square = BoxConstraints.tightFor(width: 100.0, height: 100.0);
+ final RenderConstrainedBox box1 = RenderConstrainedBox(
+ additionalConstraints: square,
+ child: RenderFlowBaselineTestBox()
+ ..gridCount = 1
+ ..baselinePlacer = (double height) => 10,
+ );
+ final RenderConstrainedBox box2 = RenderConstrainedBox(
+ additionalConstraints: square,
+ child: RenderFlowBaselineTestBox()
+ ..gridCount = 1
+ ..baselinePlacer = (double height) => 10,
+ );
+ RenderConstrainedBox filler() => RenderConstrainedBox(additionalConstraints: square);
+ final RenderFlex flex = RenderFlex(
+ textDirection: TextDirection.ltr,
+ children: <RenderBox>[
+ filler(),
+ box1,
+ filler(),
+ box2,
+ filler(),
+ ],
+ direction: Axis.vertical,
+ );
+ layout(flex, phase: EnginePhase.paint);
+ final double flexHeight = flex.size.height;
+
+ // We can't call the getDistanceToBaseline method directly. Check the dry
+ // baseline instead, and in debug mode there are asserts that verify
+ // the two methods return the same results.
+ expect(flex.getDryBaseline(flex.constraints, TextBaseline.alphabetic), 100 + 10);
+
+ flex.mainAxisAlignment = MainAxisAlignment.end;
+ pumpFrame(phase: EnginePhase.paint);
+ expect(flex.getDryBaseline(flex.constraints, TextBaseline.alphabetic), flexHeight - 400 + 10);
+
+ flex.verticalDirection = VerticalDirection.up;
+ pumpFrame(phase: EnginePhase.paint);
+ expect(flex.getDryBaseline(flex.constraints, TextBaseline.alphabetic), 300 + 10);
+
+ flex.mainAxisAlignment = MainAxisAlignment.start;
+ pumpFrame(phase: EnginePhase.paint);
+ expect(flex.getDryBaseline(flex.constraints, TextBaseline.alphabetic), flexHeight - 200 + 10);
+ });
+
group('Intrinsics', () {
test('main axis intrinsics with RenderAspectRatio 1', () {
const BoxConstraints square = BoxConstraints.tightFor(width: 100.0, height: 100.0);