applyParentData method

  1. @override
void applyParentData(
  1. RenderObject renderObject
)
override

Write the data from this widget into the given render object's parent data.

The framework calls this function whenever it detects that the RenderObject associated with the child has outdated RenderObject.parentData. For example, if the render object was recently inserted into the render tree, the render object's parent data might not match the data in this widget.

Subclasses are expected to override this function to copy data from their fields into the RenderObject.parentData field of the given render object. The render object's parent is guaranteed to have been created by a widget of type T, which usually means that this function can assume that the render object's parent data object inherits from a particular class.

If this function modifies data that can change the parent's layout or painting, this function is responsible for calling RenderObject.markNeedsLayout or RenderObject.markNeedsPaint on the parent, as appropriate.

Implementation

@override
void applyParentData(RenderObject renderObject) {
  assert(renderObject.parentData is StackParentData);
  final StackParentData parentData = renderObject.parentData! as StackParentData;
  bool needsLayout = false;

  if (parentData.left != left) {
    parentData.left = left;
    needsLayout = true;
  }

  if (parentData.top != top) {
    parentData.top = top;
    needsLayout = true;
  }

  if (parentData.right != right) {
    parentData.right = right;
    needsLayout = true;
  }

  if (parentData.bottom != bottom) {
    parentData.bottom = bottom;
    needsLayout = true;
  }

  if (parentData.width != width) {
    parentData.width = width;
    needsLayout = true;
  }

  if (parentData.height != height) {
    parentData.height = height;
    needsLayout = true;
  }

  if (needsLayout) {
    final RenderObject? targetParent = renderObject.parent;
    if (targetParent is RenderObject) {
      targetParent.markNeedsLayout();
    }
  }
}