expandNode<T> static method
Incrementally expand a node: insert its flattened subtree right after it.
Mutates currentList in place (the caller — the Flattener — owns it) to
avoid copying the whole list on every toggle, and returns that same list
with the index of the expanded node. Returns null if the node was not
found (caller should fall back to a full rebuild).
Implementation
static ({List<FlatNode<T>> list, int index})? expandNode<T>({
required List<FlatNode<T>> currentList,
required String nodeId,
required Set<String>? expandedNodeIds,
}) {
// Find the node's index in the flat list
final idx = currentList.indexWhere((fn) => fn.node.id == nodeId);
if (idx == -1) return null;
final flatNode = currentList[idx];
final node = flatNode.node;
if (node.children.isEmpty) return (list: currentList, index: idx);
// Flatten only this node's children subtree. This node is the ancestor at
// index [flatNode.depth] for those children.
final subtree = <FlatNode<T>>[];
_flattenInto<T>(
result: subtree,
nodes: node.children,
expandedNodeIds: expandedNodeIds,
depth: flatNode.depth + 1,
isRoot: false,
ancestorIsLastMask: _childAncestorMask(
flatNode.ancestorIsLastMask, flatNode.depth, flatNode.isLast),
);
// Insert the subtree after the node, in place.
currentList.insertAll(idx + 1, subtree);
return (list: currentList, index: idx);
}