distributeNodesVertically method

void distributeNodesVertically(
  1. List<String> nodeIds
)

Distributes nodes evenly along the vertical axis.

Requires at least 3 nodes. Sorts nodes by Y position, keeps the topmost and bottommost nodes in place, and distributes the middle nodes evenly.

Parameters:

  • nodeIds: List of node IDs to distribute (must contain at least 3 nodes)

Does nothing if fewer than 3 valid nodes are provided.

Example:

controller.distributeNodesVertically(['node1', 'node2', 'node3', 'node4']);

Implementation

void distributeNodesVertically(List<String> nodeIds) {
  if (nodeIds.length < 3) return;

  final nodes = nodeIds.map((id) => _nodes[id]).whereType<Node<T>>().toList();
  if (nodes.length < 3) return;

  // Sort nodes by Y position
  nodes.sort((a, b) => a.position.value.dy.compareTo(b.position.value.dy));

  final topmost = nodes.first.position.value.dy;
  final bottommost = nodes.last.position.value.dy;
  final spacing = (bottommost - topmost) / (nodes.length - 1);

  runInAction(() {
    for (int i = 1; i < nodes.length - 1; i++) {
      final targetY = topmost + spacing * i;
      final newPosition = Offset(nodes[i].position.value.dx, targetY);
      nodes[i].position.value = newPosition;
      // Update visual position with snapping
      nodes[i].setVisualPosition(_config.snapToGridIfEnabled(newPosition));
    }
  });
}