getPortPosition method

Offset getPortPosition(
  1. String portId, {
  2. required Size portSize,
  3. NodeShape? shape,
})

Gets the connection point for a port where line endpoints should attach.

Connections attach at the port's outer edge since ports are edge-aligned with the node/shape boundary. This method returns absolute coordinates in the graph coordinate space.

Parameters:

  • portId - The unique identifier of the port
  • portSize - The size of the port widget (width x height)
  • shape - Optional shape to use for port position calculation

Returns the absolute Offset where connection lines should attach.

Throws ArgumentError if no port with the given portId is found.

Implementation

Offset getPortPosition(
  String portId, {
  required Size portSize,
  NodeShape? shape,
}) {
  final port = [
    ...inputPorts,
    ...outputPorts,
  ].cast<Port?>().firstWhere((p) => p?.id == portId, orElse: () => null);

  if (port == null) {
    throw ArgumentError('Port $portId not found');
  }

  // Calculate connection point offset based on port position.
  // Connections attach at the port's outer edge (aligned with node/shape boundary):
  // - Left: left edge of port widget (x=0, y=center)
  // - Right: right edge of port widget (x=portSize.width, y=center)
  // - Top: top edge of port widget (x=center, y=0)
  // - Bottom: bottom edge of port widget (x=center, y=portSize.height)
  final Offset connectionOffset = switch (port.position) {
    PortPosition.left => Offset(0, portSize.height / 2),
    PortPosition.right => Offset(portSize.width, portSize.height / 2),
    PortPosition.top => Offset(portSize.width / 2, 0),
    PortPosition.bottom => Offset(portSize.width / 2, portSize.height),
  };

  // Convert from node coordinates to absolute graph coordinates
  return visualPosition.value +
      getVisualPortPosition(portId, portSize: portSize, shape: shape) +
      connectionOffset;
}