replaceChildAt method

  1. @override
void replaceChildAt(
  1. DisplayObject child,
  2. int index
)
override

Replaces the child at the specified index position with the new child. The current child at this position is removed.

The parent property of the removed child is set to null, and the object is garbage collected if no other references to the child exist.

Implementation

@override
void replaceChildAt(DisplayObject child, int index) {
  if (index < 0 || index >= _children.length) {
    throw ArgumentError('The supplied index is out of bounds.');
  } else if (child == this) {
    throw ArgumentError('An object cannot be added as a child of itself.');
  } else if (child.parent == this) {
    if (_children.indexOf(child) == index) return;
    throw ArgumentError(
        'The display object is already a child of this container.');
  } else {
    child.removeFromParent();
    _throwIfAncestors(child);
    _clearChildParent(_children[index]);
    _children[index] = child;
    _setChildParent(child);
  }
}