replaceItems method
Replaces tree content while preserving state for IDs that remain.
Implementation
void replaceItems(Iterable<TreeItem<T>> items) {
final replacement = List<TreeItem<T>>.unmodifiable(items);
_validateUniqueIds(replacement);
final previousSelection = selectedItem?.id;
final previousIds = _allIds(_items);
_items = replacement;
for (final item in _walkItems(replacement)) {
if (!previousIds.contains(item.id) && item.initiallyExpanded) {
_expandedIds.add(item.id);
}
}
_expandedIds.removeWhere((id) => !_containsId(replacement, id));
final projected = _projectRows();
final selectedIndex = previousSelection == null
? -1
: projected.indexWhere((row) => row.item.id == previousSelection);
_cursor = selectedIndex >= 0
? selectedIndex
: _cursor.clamp(-1, projected.length - 1);
if (_cursor < 0) _cursor = _firstSelectableIndex(projected);
_clampState();
}