swap method
Swap two nodes in the tree. This moves node1 to the position of node2 and node2 to the position of node1.
This method will throw if node1 and node2 are not attached to the tree.
Implementation
void swap(TreeNode node1, TreeNode node2, {bool notify = true}) {
assert(node1.isAttached && node2.isAttached, '''
Nodes must be attached in order for it to be able to be moved
''');
final node1Siblings = node1.siblings;
final node2Siblings = node2.siblings;
final node1Index = node1.index;
final node2Index = node2.index;
final node1Parent = node1.parent;
final node2Parent = node2.parent;
node1Siblings.remove(node1);
node1Siblings.insert(node1Index, node2);
node2Siblings.remove(node2);
node2Siblings.insert(node2Index, node1);
node1._parent = node2Parent;
node2._parent = node1Parent;
if (_onMoved != null) {
_onMoved(node1, node2Index, node1Parent, node2Parent);
_onMoved(node2, node1Index, node2Parent, node1Parent);
}
if (_onChanged != null) {
_onChanged();
}
if (notify) notifyListeners();
}