computeDistanceToHighestActualBaseline method

double? computeDistanceToHighestActualBaseline(
  1. TextBaseline baseline
)

Baseline rule is as follows:

  1. Loop children to find baseline, if child is block-level find the nearest non block-level children's height as baseline
  2. If child is text-box, use text's baseline

Implementation

double? computeDistanceToHighestActualBaseline(TextBaseline baseline) {
  double? result;
  RenderBox? child = firstChild;
  while (child != null) {
    final RenderLayoutParentData childParentData =
        child.parentData as RenderLayoutParentData;

    // Whether child is inline-level including text box
    bool isChildInline = true;
    if (child is RenderBoxModel) {
      CSSDisplay? childTransformedDisplay =
          child.renderStyle.effectiveDisplay;
      if (childTransformedDisplay == CSSDisplay.block ||
          childTransformedDisplay == CSSDisplay.flex) {
        isChildInline = false;
      }
    }

    // Block level and positioned element doesn't involve in baseline alignment
    if (childParentData.isPositioned) {
      child = childParentData.nextSibling;
      continue;
    }

    double? childDistance = child.getDistanceToActualBaseline(baseline);
    // Use child's height if child has no baseline and not block-level
    // Text box always has baseline
    if (childDistance == null &&
        isChildInline &&
        child is RenderBoxModel) {
      // Flutter only allow access size of direct children, so cannot use child.size
      Size childSize = child.getBoxSize(child.contentSize);
      childDistance = childSize.height;
    }

    if (childDistance != null) {
      childDistance += childParentData.offset.dy;
      if (result != null)
        result = math.min(result, childDistance);
      else
        result = childDistance;
    }
    child = childParentData.nextSibling;
  }
  return result;
}