selectNode method

void selectNode(
  1. String nodeId, {
  2. bool toggle = false,
})

Selects a node in the graph.

Automatically clears selections of other element types (connections, annotations). Requests canvas focus if not already focused.

Triggers the onNodeSelected callback after selection changes.

Parameters:

  • nodeId: The ID of the node to select
  • toggle: If true, toggles the node's selection state. If false (default), clears other node selections and selects only this node.

Example:

// Select single node
controller.selectNode('node1');

// Toggle node selection (for multi-select)
controller.selectNode('node2', toggle: true);

Implementation

void selectNode(String nodeId, {bool toggle = false}) {
  runInAction(() {
    // Clear other element types' selections
    clearConnectionSelection();
    annotations.clearAnnotationSelection();

    if (toggle) {
      if (_selectedNodeIds.contains(nodeId)) {
        _selectedNodeIds.remove(nodeId);
        final node = _nodes[nodeId];
        if (node != null) {
          node.selected.value = false;
        }
      } else {
        _selectedNodeIds.add(nodeId);
        final node = _nodes[nodeId];
        if (node != null) {
          node.selected.value = true;
        }
      }
    } else {
      // Clear previous node selection
      clearNodeSelection();

      // Select new node
      _selectedNodeIds.add(nodeId);
      final node = _nodes[nodeId];
      if (node != null) {
        node.selected.value = true;
      }
    }
  });

  // Fire selection callback with current selection state
  final selectedNode = _selectedNodeIds.contains(nodeId)
      ? _nodes[nodeId]
      : null;
  callbacks.onNodeSelected?.call(selectedNode);

  if (!canvasFocusNode.hasFocus) {
    canvasFocusNode.requestFocus();
  }
}