computeDistanceToBaseline method

  1. @override
double? computeDistanceToBaseline()
override

Compute distance to baseline

Implementation

@override
double? computeDistanceToBaseline() {
  double lineDistance = 0;
  double marginTop = renderStyle.marginTop.computedValue;
  double marginBottom = renderStyle.marginBottom.computedValue;
  bool isParentFlowLayout = parent is RenderFlowLayout;
  CSSDisplay? effectiveDisplay = renderStyle.effectiveDisplay;
  bool isDisplayInline = effectiveDisplay != CSSDisplay.block &&
      effectiveDisplay != CSSDisplay.flex;
  // Use margin bottom as baseline if layout has no children
  if (_flexLineBoxMetrics.isEmpty) {
    if (isDisplayInline) {
      // Flex item baseline does not includes margin-bottom
      lineDistance = isParentFlowLayout
          ? marginTop + boxSize!.height + marginBottom
          : marginTop + boxSize!.height;
      return lineDistance;
    } else {
      return null;
    }
  }

  // Always use the baseline of the first child as the baseline in flex layout.
  _RunMetrics firstLineMetrics = _flexLineBoxMetrics[0];
  List<_RunChild> firstRunChildren =
      firstLineMetrics.runChildren.values.toList();
  _RunChild firstRunChild = firstRunChildren[0];
  RenderBox child = firstRunChild.child;

  double childMarginTop =
      child is RenderBoxModel ? child.renderStyle.marginTop.computedValue : 0;
  RenderLayoutParentData childParentData =
      child.parentData as RenderLayoutParentData;
  double? childBaseLineDistance = 0;
  if (child is RenderBoxModel) {
    childBaseLineDistance = child.computeDistanceToBaseline();
  } else if (child is RenderTextBox) {
    childBaseLineDistance = child.computeDistanceToFirstLineBaseline();
  }

  // Baseline of relative positioned element equals its original position
  // so it needs to subtract its vertical offset.
  Offset? relativeOffset;
  double childOffsetY = childParentData.offset.dy - childMarginTop;
  if (child is RenderBoxModel) {
    relativeOffset =
        CSSPositionedLayout.getRelativeOffset(child.renderStyle);
  }
  if (relativeOffset != null) {
    childOffsetY -= relativeOffset.dy;
  }

  // It needs to subtract margin-top cause offset already includes margin-top.
  lineDistance = (childBaseLineDistance ?? 0) + childOffsetY;
  lineDistance += marginTop;
  return lineDistance;
}