update method

  1. @override
void update(
  1. covariant SliverTree<TKey, TData> newWidget
)
override

Change the widget used to configure this element.

The framework calls this function when the parent wishes to use a different widget to configure this element. The new widget is guaranteed to have the same runtimeType as the old widget.

This function is called only during the "active" lifecycle state.

Implementation

@override
void update(SliverTree<TKey, TData> newWidget) {
  final oldWidget = widget;
  super.update(newWidget);

  if (oldWidget.controller != newWidget.controller) {
    oldWidget.controller.removeStructuralListener(_onStructuralChange);
    oldWidget.controller.removeAnimationListener(_onAnimationTick);
    oldWidget.controller.removeNodeDataListener(_onNodeDataChanged);
    newWidget.controller.addStructuralListener(_onStructuralChange);
    newWidget.controller.addAnimationListener(_onAnimationTick);
    newWidget.controller.addNodeDataListener(_onNodeDataChanged);
    renderObject.controller = newWidget.controller;
    // Old-controller keys are meaningless under the new controller;
    // drop the queue and let createChild rebuild against fresh data.
    _dirtyKeys.clear();
    // Schedule a GC pass: keys that existed under the old controller
    // but not under the new one would otherwise survive as stale
    // elements (the renderObject's _children map was cleared in the
    // controller setter, but the Elements themselves remain in
    // _children, still in the widget tree, with no live nodeData).
    // Without this, a parent rebuild can swap controllers and leave
    // dead rows participating in find/semantics/focus until the next
    // structural notification on the new controller fires GC.
    _scheduleGarbageCollection();
  }

  if (_didReassemble) {
    _didReassemble = false;
    _invalidateAllChildren();
    renderObject.markStructureChanged();
    return;
  }

  // Parent rebuild: the `nodeBuilder` closure may have captured new
  // state. Queue every mounted row for lazy refresh and trigger a
  // layout pass — retained rows rebuild in [createChild] within the
  // same frame; off-screen rows wait for re-entry or discard. This
  // bounds the rebuild fan-out to the retained set (≈ cache region),
  // not the full mounted set. See [_dirtyKeys] doc.
  _dirtyKeys.addAll(_children.keys);
  renderObject.markNeedsLayout();
}