sendNodeBackward method
Moves a node one step backward in the z-order.
Swaps the z-index with the next lower node in the visual stack. Does nothing if the node is already at the back.
Parameters:
nodeId: The ID of the node to move backward
Example:
controller.sendNodeBackward('node1');
Implementation
void sendNodeBackward(String nodeId) {
final node = _nodes[nodeId];
if (node == null) return;
runInAction(() {
// Sort all nodes by z-index to get the visual order
final sortedNodes = _nodes.values.toList()
..sort((a, b) => a.zIndex.value.compareTo(b.zIndex.value));
// Find current node's position in the visual order
final currentIndex = sortedNodes.indexOf(node);
// If not at the bottom, swap with next lower node
if (currentIndex > 0) {
final prevNode = sortedNodes[currentIndex - 1];
// We need to ensure the z-indexes are different
// If they're the same, normalize all z-indexes first
if (node.zIndex.value == prevNode.zIndex.value) {
// Normalize all z-indexes to be sequential
for (int i = 0; i < sortedNodes.length; i++) {
sortedNodes[i].zIndex.value = i;
}
// Now swap the normalized values
node.zIndex.value = currentIndex - 1;
prevNode.zIndex.value = currentIndex;
} else {
// Z-indexes are different, just swap them
final currentZ = node.zIndex.value;
final prevZ = prevNode.zIndex.value;
node.zIndex.value = prevZ;
prevNode.zIndex.value = currentZ;
}
}
});
}