getContentSize method

Size getContentSize({
  1. required double contentWidth,
  2. required double contentHeight,
})

Common layout content size (including flow and flexbox layout) calculation logic

Implementation

Size getContentSize({
  required double contentWidth,
  required double contentHeight,
}) {
  double finalContentWidth = contentWidth;
  double finalContentHeight = contentHeight;

  // Size which is specified by sizing styles
  double? specifiedContentWidth = renderStyle.contentBoxLogicalWidth;
  double? specifiedContentHeight = renderStyle.contentBoxLogicalHeight;

  // Flex basis takes priority over main size in flex item when flex-grow or flex-shrink not work.
  if (parent is RenderFlexLayout) {
    RenderBoxModel? parentRenderBoxModel = parent as RenderBoxModel?;
    double? flexBasis = renderStyle.flexBasis == CSSLengthValue.auto ? null : renderStyle.flexBasis?.computedValue;
    if (flexBasis != null) {
      if (CSSFlex.isHorizontalFlexDirection(
          parentRenderBoxModel!.renderStyle.flexDirection)) {
        if (!hasOverrideContentLogicalWidth) {
          specifiedContentWidth = _getContentWidth(flexBasis);
        }
      } else {
        if (!hasOverrideContentLogicalHeight) {
          specifiedContentHeight = _getContentHeight(flexBasis);
        }
      }
    }
  }

  if (specifiedContentWidth != null) {
    finalContentWidth = math.max(specifiedContentWidth, contentWidth);
  }
  if (specifiedContentHeight != null) {
    finalContentHeight = math.max(specifiedContentHeight, contentHeight);
  }

  CSSDisplay? effectiveDisplay = renderStyle.effectiveDisplay;
  bool isInlineBlock = effectiveDisplay == CSSDisplay.inlineBlock;
  bool isNotInline = effectiveDisplay != CSSDisplay.inline;
  double? width = renderStyle.width.isAuto ? null : renderStyle.width.computedValue;
  double? height = renderStyle.height.isAuto ? null : renderStyle.height.computedValue;
  double? minWidth = renderStyle.minWidth.isAuto ? null : renderStyle.minWidth.computedValue;
  double? maxWidth = renderStyle.maxWidth.isNone ? null : renderStyle.maxWidth.computedValue;
  double? minHeight = renderStyle.minHeight.isAuto ? null : renderStyle.minHeight.computedValue;
  double? maxHeight = renderStyle.maxHeight.isNone ? null : renderStyle.maxHeight.computedValue;

  // Constrain to min-width or max-width if width not exists.
  if (isInlineBlock && maxWidth != null && width == null) {
    double maxContentWidth = _getContentWidth(maxWidth);
    finalContentWidth = finalContentWidth > maxContentWidth ? maxContentWidth : finalContentWidth;
  } else if (isInlineBlock && minWidth != null && width == null) {
    double minContentWidth = _getContentWidth(minWidth);
    finalContentWidth = finalContentWidth < minContentWidth ? minContentWidth : finalContentWidth;
  }

  // Constrain to min-height or max-height if height not exists.
  if (isNotInline && maxHeight != null && height == null) {
    double maxContentHeight = _getContentHeight(maxHeight);
    finalContentHeight = finalContentHeight > maxContentHeight ? maxContentHeight : finalContentHeight;
  } else if (isNotInline && minHeight != null && height == null) {
    double minContentHeight = _getContentWidth(minHeight);
    finalContentHeight = finalContentHeight < minContentHeight ? minContentHeight : finalContentHeight;
  }

  Size finalContentSize = Size(finalContentWidth, finalContentHeight);
  return finalContentSize;
}