collapsedMarginBottom property

double collapsedMarginBottom

Implementation

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

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

  double _marginBottom;

  // Margin is invalid for inline element.
  if (effectiveDisplay == CSSDisplay.inline) {
    _marginBottom = 0;
    // Cache computed value.
    cacheComputedValue(hashCode, propertyName, _marginBottom);
    return _marginBottom;
  }

  // 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)) {
    _marginBottom = marginBottom.computedValue;
    // Cache computed value.
    cacheComputedValue(hashCode, propertyName, _marginBottom);
    return _marginBottom;
  }

  RenderLayoutParentData childParentData = boxModel.parentData as RenderLayoutParentData;
  RenderObject? nextSibling =
      childParentData.nextSibling != null ? childParentData.nextSibling as RenderObject : null;

  if (nextSibling == null) {
    // Margin bottom collapse with its parent if it is the last child of its parent and its value is 0.
    _marginBottom = _collapsedMarginBottomWithParent;
  } else {
    // Margin bottom collapse with its nested last child when meeting following cases at the same time:
    // 1. No padding, border is set.
    // 2. No height, min-height, max-height is set.
    // 3. No block formatting context of itself (eg. overflow scroll and position absolute) is created.
    _marginBottom = _collapsedMarginBottomWithLastChild;
  }

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