setSections method
void
setSections(})
Replaces all sections. Diffs against current state and animates inserts, removes, and reparenting.
itemsOf is invoked once per sections entry to materialize the
section's items. Callers may pass any Iterable (e.g., a
where/map chain); this method consumes it eagerly.
Implementation
void setSections(
Iterable<Section> sections, {
required Iterable<Item> Function(Section section) itemsOf,
bool animate = true,
}) {
_checkNotDisposed();
// Re-initialize the sync controller's tracking so the diff is
// computed against the actual current tree state. Necessary because
// direct mutations via this controller (addItem, removeItem, ...)
// bypass the sync controller's bookkeeping; without this, a
// setSections after such mutations would diff against a stale
// baseline and fail to remove drifted nodes.
_sync.initializeTracking();
final list = sections.toList(growable: false);
final desired = <TreeNode<SecKey<K>, SecPayload<Section, Item>>>[
for (final s in list)
TreeNode(
key: SectionKey<K>(sectionKeyOf(s)),
data: SectionPayload<Section, Item>(s),
),
];
final byKey = <K, Section>{for (final s in list) sectionKeyOf(s): s};
_sync.syncRoots(
desired,
childrenOf: (k) {
if (k is! SectionKey<K>) {
return const [];
}
final section = byKey[k.value];
if (section == null) {
return const [];
}
return <TreeNode<SecKey<K>, SecPayload<Section, Item>>>[
for (final i in itemsOf(section))
TreeNode(
key: ItemKey<K>(itemKeyOf(i)),
data: ItemPayload<Section, Item>(i),
),
];
},
animate: animate,
);
}