dryLastBaselineOf function
The lowest baseline of an in-flow line in box, from its top, asked of
a box that has not been laid out.
A flex line asks each item where its baseline is BEFORE it lays anything out (css-flexbox-1 8.5), and a paragraph asks the same of a placeholder it is measuring -- and Flutter answers an intrinsic height the same way. Answered with the FIRST baseline -- which is what a proxy forwards -- an inline-block hangs its whole column from its first line, and the line it stands on comes out one leading too tall: forty of a news site's story links did that, 3px each.
A paragraph keeps its lines to itself and a dry pass has no caret to put at the end of them; under a FORCED strut, which is how a line of lowered text is set, every line puts its baseline the same distance down, so the last one's is the first one's plus every line above it.
Anywhere else the answer has to come from the box itself: a box knows what it offers its child and where it puts it, and nothing outside it does. DryLastBaseline is that answer, and a box that comes to exactly its child's size at the same constraints needs no answer of its own -- it has no edge under the line and hands the offer straight down. Where neither holds, the FIRST baseline is what the box already reports and is the only number that is certainly not invented.
Implementation
double? dryLastBaselineOf(RenderBox box, BoxConstraints constraints, TextBaseline baseline) {
// An `IgnoreBaseline` answers none, dry as laid out
// ([RenderLastBaseline.lastBaselineOf]).
if (box is RenderIgnoreBaseline) return null;
if (box is DryLastBaseline) return box.dryLastBaseline(constraints, baseline);
// A flex or grid container gives ONE baseline, its first item's
// (css-flexbox-1 8.5, css-grid-1 11.8), and holds no line box of its own
// for a lower line to be found in.
if (box is OneBaseline) return dryBaselineOf(box, constraints, baseline);
if (box is RenderParagraph) return _lastLine(box, constraints, baseline);
// Flutter's own padding, which cannot carry the mixin: its bottom edge
// hangs under the last line.
if (box is RenderPadding) {
final c = box.child;
if (c == null) return null;
final e = box.padding.resolve(box.textDirection);
final b = dryLastBaselineOf(c, constraints.deflate(e), baseline);
return b == null ? null : b + e.top;
}
// Flutter's own SizedBox and ConstrainedBox, which cannot carry the mixin
// either: they offer their child their own constraints ENFORCED, put it
// at their top and come to its size, so their last line is the child's,
// asked at that offer. Asked at the offer it was handed instead, a stated
// width the words wrap in (an `inline-block` with a `width`) comes to one
// long line, is not the box's size, and the box answered its FIRST line:
// a two-line heading hung its strut from its first, and the section under
// it stood 2px high (`fixtures/inline-block-stated-width-last-line`).
// What the walk finds down there is checked against the box's first line
// ([lastNotAboveFirst]).
if (box is RenderConstrainedBox) {
final c = box.child;
if (c == null) return null;
return lastNotAboveFirst(
dryLastBaselineOf(c, box.additionalConstraints.enforce(constraints), baseline),
() => dryBaselineOf(box, constraints, baseline),
);
}
RenderBox? last;
box.visitChildren((c) {
if (c is! RenderBox) return;
final data = c.parentData;
if (data is StackParentData && data.isPositioned) return;
last = c;
});
final kid = last;
if (kid == null) return dryBaselineOf(box, constraints, baseline);
if (guardedSize(box, () => kid.getDryLayout(constraints) == box.getDryLayout(constraints))) {
return dryLastBaselineOf(kid, constraints, baseline);
}
// A BLOCK COLUMN, which is what a block flow is lowered to. The last
// child's bottom edge IS the column's, so its last line is the column's
// last line, that much above the bottom -- and the column offers it the
// width it came to itself, which is the one thing a dry pass has to know
// to ask it anything. Stacked any other way (a spacer under the last
// child, an alignment that leaves room below it) there is no telling,
// and the FIRST baseline is what the box already reports.
if (box is RenderFlex &&
box.direction == Axis.vertical &&
box.mainAxisSize == MainAxisSize.min &&
box.mainAxisAlignment == MainAxisAlignment.start) {
final size = guardedSize(box, () => box.getDryLayout(constraints));
final inner = BoxConstraints(
minWidth: box.crossAxisAlignment == CrossAxisAlignment.stretch ? size.width : 0.0,
maxWidth: size.width,
);
final b = dryLastBaselineOf(kid, inner, baseline);
if (b != null) return b + size.height - guardedSize(kid, () => kid.getDryLayout(inner)).height;
}
return dryBaselineOf(box, constraints, baseline);
}