insertBefore method

  1. @override
  2. @mustCallSuper
Node insertBefore(
  1. Node child,
  2. Node referenceNode
)
inherited

Implementation

@override
@mustCallSuper
Node insertBefore(Node child, Node referenceNode) {
  _debugCheckNestedInline(child);

  int referenceIndex = childNodes.indexOf(referenceNode);
  // Node.insertBefore will change element tree structure,
  // so get the referenceIndex before calling it.
  Node node = super.insertBefore(child, referenceNode);
  if (isRendererAttached) {
    // Only append child renderer when which is not attached.
    if (!child.isRendererAttached) {
      RenderBox? afterRenderObject;
      // `referenceNode` should not be null, or `referenceIndex` can only be -1.
      if (referenceIndex != -1 && referenceNode.isRendererAttached) {
        afterRenderObject = (referenceNode.renderer!.parentData as ContainerParentDataMixin<RenderBox>).previousSibling;
      }
      child.attachTo(this, after: afterRenderObject);
    }
  }

  return node;
}