collapsedMarginTop property

double collapsedMarginTop

Implementation

double get collapsedMarginTop {
  RenderBoxModel boxModel = renderBoxModel!;
  int hashCode = boxModel.hashCode;
  String propertyName = 'collapsedMarginTop';

  // Use cached value if exits.
  double? cachedValue = getCachedComputedValue(hashCode, propertyName);
  if (cachedValue != null) {
    return cachedValue;
  }

  double _marginTop;

  if (effectiveDisplay == CSSDisplay.inline) {
    _marginTop = 0;
    // Cache computed value.
    cacheComputedValue(hashCode, propertyName, _marginTop);
    return _marginTop;
  }

  // Margin collapse does not work on following case:
  // 1. Document root element(HTML)
  // 2. Inline level elements
  // 3. Inner renderBox of element with overflow auto/scroll
  if (boxModel.isDocumentRootBox || (effectiveDisplay != CSSDisplay.block && effectiveDisplay != CSSDisplay.flex)) {
    _marginTop = marginTop.computedValue;
    // Cache computed value.
    cacheComputedValue(hashCode, propertyName, _marginTop);
    return _marginTop;
  }
  RenderLayoutParentData childParentData = boxModel.parentData as RenderLayoutParentData;
  RenderObject? preSibling =
      childParentData.previousSibling != null ? childParentData.previousSibling as RenderObject : null;

  if (preSibling == null) {
    // Margin top collapse with its parent if it is the first child of its parent and its value is 0.
    _marginTop = _collapsedMarginTopWithParent;
  } else {
    // Margin top collapse with margin-bottom of its previous sibling, get the difference between
    // the margin top of itself and the margin bottom of ite previous sibling. Set it to 0 if the
    // difference is negative.
    _marginTop = _collapsedMarginTopWithPreSibling;
  }

  // Cache computed value.
  cacheComputedValue(hashCode, propertyName, _marginTop);
  return _marginTop;
}