arrangeNodesInGrid method

void arrangeNodesInGrid({
  1. double spacing = 150.0,
})

Arranges all nodes in a grid layout.

Calculates an optimal grid size based on the square root of the number of nodes and positions them in rows and columns with the specified spacing.

Parameters:

  • spacing: The distance between nodes in pixels (default: 150.0)

Example:

controller.arrangeNodesInGrid(spacing: 200.0);

Implementation

void arrangeNodesInGrid({double spacing = 150.0}) {
  final nodeList = _nodes.values.toList();
  final gridSize = math.sqrt(nodeList.length.toDouble()).ceil();

  runInAction(() {
    for (int i = 0; i < nodeList.length; i++) {
      final row = i ~/ gridSize;
      final col = i % gridSize;
      final newPosition = Offset(col * spacing, row * spacing);
      nodeList[i].position.value = newPosition;
      // Update visual position with snapping
      nodeList[i].setVisualPosition(_config.snapToGridIfEnabled(newPosition));
    }
  });
}