Renderflex cross intrinsic size with baseline alignment (#146185)

This commit is contained in:
LongCatIsLooong
2024-04-03 10:41:50 -07:00
committed by GitHub
parent 5aaee98b75
commit a81729c68e
2 changed files with 55 additions and 60 deletions

View File

@@ -595,18 +595,6 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
required double extent, // the extent in the direction that isn't the sizing direction
required _ChildSizingFunction childSize, // a method to find the size in the sizing direction
}) {
// TODO(LongCatIsLooong): use _computeSizes for intrinsics calculation and
// add support for baseline alignment.
if (_isBaselineAligned) {
// Intrinsics cannot be calculated without a full layout for
// baseline alignment. Throw an assertion and return 0.0 as documented
// on [RenderBox.computeMinIntrinsicWidth].
assert(
RenderObject.debugCheckingIntrinsics,
'Intrinsics are not available for CrossAxisAlignment.baseline.',
);
return 0.0;
}
if (_direction == sizingDirection) {
// INTRINSIC MAIN SIZE
// Intrinsic main size is the smallest size the flex container can take
@@ -614,18 +602,15 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
double totalFlex = 0.0;
double inflexibleSpace = 0.0;
double maxFlexFractionSoFar = 0.0;
RenderBox? child = firstChild;
while (child != null) {
for (RenderBox? child = firstChild; child != null; child = childAfter(child)) {
final int flex = _getFlex(child);
totalFlex += flex;
if (flex > 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<RenderBox, Fl
// Intrinsic cross size is the max of the intrinsic cross sizes of the
// children, after the flexible children are fit into the available space,
// with the children sized using their max intrinsic dimensions.
final bool isHorizontal = switch (direction) {
Axis.horizontal => 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;
}
}

View File

@@ -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: <RenderBox>[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()