getConstraints method

BoxConstraints getConstraints()

Implementation

BoxConstraints getConstraints() {
  // Inner scrolling content box of overflow element inherits constraints from parent
  // but has indefinite max constraints to allow children overflow
  if (isScrollingContentBox) {
    RenderStyle parentRenderStyle = (parent as RenderBoxModel).renderStyle;
    EdgeInsets borderEdge = parentRenderStyle.border;
    EdgeInsetsGeometry? padding = parentRenderStyle.padding;
    double horizontalBorderLength = borderEdge.horizontal;
    double verticalBorderLength = borderEdge.vertical;
    double horizontalPaddingLength = padding.horizontal;
    double verticalPaddingLength = padding.vertical;

    BoxConstraints parentConstraints = (parent as RenderBoxModel).constraints;
    BoxConstraints constraints = BoxConstraints(
      minWidth: parentConstraints.maxWidth != double.infinity ?
        parentConstraints.maxWidth - horizontalBorderLength - horizontalPaddingLength : 0,
      maxWidth: double.infinity,
      minHeight: parentConstraints.maxHeight != double.infinity ?
        parentConstraints.maxHeight - verticalBorderLength - verticalPaddingLength : 0,
      maxHeight: double.infinity,
    );
    return constraints;
  }

  CSSDisplay? effectiveDisplay = renderStyle.effectiveDisplay;
  bool isDisplayInline = effectiveDisplay == CSSDisplay.inline;

  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;

  // Need to calculated logic content size on every layout.
  renderStyle.computeContentBoxLogicalWidth();
  renderStyle.computeContentBoxLogicalHeight();

  // Width should be not smaller than border and padding in horizontal direction
  // when box-sizing is border-box which is only supported.
  double minConstraintWidth = renderStyle.effectiveBorderLeftWidth.computedValue
    + renderStyle.effectiveBorderRightWidth.computedValue
    + renderStyle.paddingLeft.computedValue
    + renderStyle.paddingRight.computedValue;
  double maxConstraintWidth = renderStyle.borderBoxLogicalWidth ?? double.infinity;
  // Height should be not smaller than border and padding in vertical direction
  // when box-sizing is border-box which is only supported.
  double minConstraintHeight = renderStyle.effectiveBorderTopWidth.computedValue
    + renderStyle.effectiveBorderBottomWidth.computedValue
    + renderStyle.paddingTop.computedValue
    + renderStyle.paddingBottom.computedValue;
  double maxConstraintHeight = renderStyle.borderBoxLogicalHeight ?? double.infinity;

  if (parent is RenderFlexLayout) {
    double? flexBasis = renderStyle.flexBasis == CSSLengthValue.auto
      ? null : renderStyle.flexBasis?.computedValue;
    RenderBoxModel? parentRenderBoxModel = parent as RenderBoxModel?;
    // In flex layout, flex basis takes priority over width/height if set.
    // Flex-basis cannot be smaller than its content size which happens can not be known
    // in constraints apply stage, so flex-basis acts as min-width in constraints apply stage.
    if (flexBasis != null) {
      if (CSSFlex.isHorizontalFlexDirection(
          parentRenderBoxModel!.renderStyle.flexDirection)) {
        minWidth = minWidth != null
          ? math.max(flexBasis, minWidth) : flexBasis;
      } else {
        minHeight = minHeight != null
          ? math.max(flexBasis, minHeight) : flexBasis;
      }
    }
  }

  // Clamp constraints by min/max size when display is not inline.
  if (!isDisplayInline) {
    if (minWidth != null) {
      minConstraintWidth = minConstraintWidth < minWidth
        ? minWidth : minConstraintWidth;
      maxConstraintWidth = maxConstraintWidth < minWidth
        ? minWidth : maxConstraintWidth;
    }
    if (maxWidth != null) {
      minConstraintWidth = minConstraintWidth > maxWidth
        ? maxWidth : minConstraintWidth;
      maxConstraintWidth = maxConstraintWidth > maxWidth
        ? maxWidth : maxConstraintWidth;
    }
    if (minHeight != null) {
      minConstraintHeight = minConstraintHeight < minHeight
        ? minHeight : minConstraintHeight;
      maxConstraintHeight = maxConstraintHeight < minHeight
        ? minHeight : maxConstraintHeight;
    }
    if (maxHeight != null) {
      minConstraintHeight = minConstraintHeight > maxHeight
        ? maxHeight : minConstraintHeight;
      maxConstraintHeight = maxConstraintHeight > maxHeight
        ? maxHeight : maxConstraintHeight;
    }
  }

  BoxConstraints constraints = BoxConstraints(
    minWidth: minConstraintWidth,
    maxWidth: maxConstraintWidth,
    minHeight: minConstraintHeight,
    maxHeight: maxConstraintHeight,
  );
  return constraints;
}