lastBaselineOf static method

double? lastBaselineOf(
  1. RenderBox box,
  2. TextBaseline baseline
)

The lowest baseline of an in-flow line in box, from its top: in a paragraph its last line's, and in a box of boxes the lowest of theirs. A positioned child is out of the flow and has no say.

A paragraph keeps its lines to itself, so its last line is found by the caret at the end of its text, and stood on as the first line is: under a forced strut, which is how a line of lowered text is set, every line puts its baseline the same distance down.

Implementation

static double? lastBaselineOf(RenderBox box, TextBaseline baseline) {
  // AN IGNORED BASELINE IS NONE: what stands in an `IgnoreBaseline` has
  // no say in a baseline above it, here as in Flutter's own boxes. A news site's
  // strip links stand an svg icon in one, and the walk that went past it
  // asked the icon.
  if (box is RenderIgnoreBaseline) return null;
  // NOR HAS A BOX NOT LAID OUT YET: a subtree its own relayout boundary
  // lays out later in the frame -- the news site's icon, its picture just arrived
  // -- has no line to stand on, and asked one it throws (it has no
  // constraints).
  if (!box.hasSize) return null;
  // THE WALK READS BOXES IT IS NOT THE PARENT OF, and asks each the way a
  // parent does, through its cache: a box asked its baseline that is laid
  // out again lays out again what asked it, past a relayout boundary
  // ([RenderBox.markNeedsLayout]). Read around the cache, a line under a
  // boundary whose text changed was laid out after the block standing on
  // it, so the walk read it stale -- the news site's section frames at 360, a
  // paragraph asked before it was laid out.
  final first = box.getDistanceToActualBaseline(baseline);
  if (box is RenderEdgeBaseline) return first;
  // An inline formatting context knows its own last line.
  if (box is RenderInlineFlow) return box.lastBaseline;
  // A flex container gives one baseline, its first line's, and holds no
  // line box of its own for the walk below to find a lower one in.
  if (box is OneBaseline) return first;
  // A LINE BOX PAINTS ITS PARAGRAPH where CSS puts the baseline, not
  // where Skia rounded it to, and keeps that drop out of the parent data
  // the walk below reads: a web portal's 20.5px inline-block links answered 14 for
  // a line whose baseline is 15 -- 15 asked dry -- and the list rows
  // round them came out 21 (`fixtures/inline-block-link-row`).
  if (box is RenderLineBox) {
    // Lines read off the boxes on them: where CSS puts the last one.
    if (box.below case final below?) return first == null ? null : first + below;
    final child = box.child;
    final b = child == null ? null : lastBaselineOf(child, baseline);
    return b == null ? null : b + box.drop;
  }
  if (box is RenderParagraph) {
    if (first == null) return null;
    final end = TextPosition(offset: box.text.toPlainText(includeSemanticsLabels: false).length);
    final down =
        box.getOffsetForCaret(end, Rect.zero).dy - box.getOffsetForCaret(const TextPosition(offset: 0), Rect.zero).dy;
    return first + down;
  }
  double? lowest;
  var any = false;
  box.visitChildren((c) {
    if (c is! RenderBox) return;
    any = true;
    final data = c.parentData;
    if (data is StackParentData && data.isPositioned) return;
    final b = lastBaselineOf(c, baseline);
    if (b == null) return;
    final at = (data is BoxParentData ? data.offset.dy : 0.0) + b;
    if (lowest == null || at > lowest!) lowest = at;
  });
  return any ? lowest : first;
}