getRelativeOffset static method

Offset? getRelativeOffset(
  1. RenderStyle renderStyle
)

Implementation

static Offset? getRelativeOffset(RenderStyle renderStyle) {
  CSSLengthValue left = renderStyle.left;
  CSSLengthValue right = renderStyle.right;
  CSSLengthValue top = renderStyle.top;
  CSSLengthValue bottom = renderStyle.bottom;
  if (renderStyle.position == CSSPositionType.relative) {
    double? dx;
    double? dy;

    // CSS2.1 ยง9.4.3 (Relative positioning): If both 'left' and 'right' are
    // specified as non-auto values, the 'direction' property determines which
    // one applies: in LTR, 'left' wins; in RTL, 'right' wins.
    final bool hasLeft = left.isNotAuto;
    final bool hasRight = right.isNotAuto;
    if (hasLeft && hasRight) {
      if (renderStyle.direction == TextDirection.rtl) {
        // RTL: use 'right' and ignore 'left'
        dx = -right.computedValue;
      } else {
        // LTR: use 'left' and ignore 'right'
        dx = left.computedValue;
      }
    } else if (hasLeft) {
      dx = left.computedValue;
    } else if (hasRight) {
      dx = -right.computedValue;
    }

    // Vertical axis: if both 'top' and 'bottom' are non-auto, 'top' wins per CSS2.1.
    final bool hasTop = top.isNotAuto;
    final bool hasBottom = bottom.isNotAuto;
    if (hasTop) {
      dy = top.computedValue;
    } else if (hasBottom) {
      dy = -bottom.computedValue;
    }

    if (dx != null || dy != null) {
      return Offset(dx ?? 0, dy ?? 0);
    }
  }
  return null;
}