onRemove method

  1. @override
void onRemove()
override

Called right before the component is removed from its parent and also before it changes parents (and is thus temporarily removed from the component tree).

This method will only run for a component that was previously mounted into a component tree. If a component was never mounted (for example, when it is removed before it had a chance to mount), then this callback will not trigger. Thus, onRemove runs if and only if there was a corresponding onMount call before.

Implementation

@override
void onRemove() {
  if (_parentSizeListener != null) {
    hitboxParent.size.removeListener(_parentSizeListener!);
  }
  _transformAncestors.forEach((t) => t.removeListener(_transformListener));

  // End all active collisions before removing from collision detection.
  // This ensures the parent component's activeCollisions is properly cleaned
  // up during processLifecycleEvents(), before collisionDetection.run()
  // processes new collisions in the same tick.
  if (_activeCollisions != null && _activeCollisions!.isNotEmpty) {
    for (final other in _activeCollisions!.toList()) {
      onCollisionEnd(other);
      other.onCollisionEnd(this);
    }
  }

  _collisionDetection?.remove(this);
  super.onRemove();
}