applyStickyChildOffset static method

void applyStickyChildOffset(
  1. RenderBoxModel scrollContainer,
  2. RenderBoxModel child
)

Set sticky child offset according to scroll offset and direction, when axisDirection param is null compute the both axis direction. Sticky positioning is similar to relative positioning except the offsets are automatically calculated in reference to the nearest scrollport. https://www.w3.org/TR/css-position-3/#stickypos-insets

Implementation

static void applyStickyChildOffset(RenderBoxModel scrollContainer, RenderBoxModel child) {
  RenderPositionPlaceholder childRenderPositionHolder = child.renderPositionPlaceholder!;
  RenderLayoutParentData childPlaceHolderParentData = childRenderPositionHolder.parentData as RenderLayoutParentData;
  // Original offset of sticky child in relative status
  Offset childOriginalOffset = childPlaceHolderParentData.offset;

  // Offset of sticky child to scroll container
  Offset childToScrollContainerOffset =
    childRenderPositionHolder.localToGlobal(Offset.zero, ancestor: scrollContainer);

  bool isVerticalFixed = false;
  bool isHorizontalFixed = false;
  RenderStyle childRenderStyle = child.renderStyle;

  if (childRenderStyle.left.isNotAuto || childRenderStyle.right.isNotAuto) {
    isHorizontalFixed = _applyStickyChildHorizontalOffset(
      scrollContainer, child, childOriginalOffset, childToScrollContainerOffset
    );
  }
  if (childRenderStyle.top.isNotAuto || childRenderStyle.bottom.isNotAuto) {
    isVerticalFixed = _applyStickyChildVerticalOffset(
      scrollContainer, child, childOriginalOffset, childToScrollContainerOffset
    );
  }

  if (isVerticalFixed || isHorizontalFixed) {
    // Change sticky status to fixed
    child.stickyStatus = StickyPositionType.fixed;
    child.markNeedsPaint();
  } else {
    // Change sticky status to relative
    if (child.stickyStatus == StickyPositionType.fixed) {
      child.stickyStatus = StickyPositionType.relative;
      // Reset child offset to its original offset
      child.markNeedsPaint();
    }
  }
}