replaceWith method

void replaceWith (Layer newLayer)

Replaces this layer with the given layer in the parent layer's child list.

Implementation

void replaceWith(Layer newLayer) {
  assert(parent != null);
  assert(attached == parent.attached);
  assert(newLayer.parent == null);
  assert(newLayer._nextSibling == null);
  assert(newLayer._previousSibling == null);
  assert(!newLayer.attached);
  newLayer._nextSibling = nextSibling;
  if (_nextSibling != null)
    _nextSibling._previousSibling = newLayer;
  newLayer._previousSibling = previousSibling;
  if (_previousSibling != null)
    _previousSibling._nextSibling = newLayer;
  assert(() {
    Layer node = this;
    while (node.parent != null)
      node = node.parent;
    assert(node != newLayer); // indicates we are about to create a cycle
    return true;
  }());
  parent.adoptChild(newLayer);
  assert(newLayer.attached == parent.attached);
  if (parent.firstChild == this)
    parent._firstChild = newLayer;
  if (parent.lastChild == this)
    parent._lastChild = newLayer;
  _nextSibling = null;
  _previousSibling = null;
  parent.dropChild(this);
  assert(!attached);
}