createGroupAnnotationAroundNodes method

GroupAnnotation createGroupAnnotationAroundNodes({
  1. required String id,
  2. required String title,
  3. required Set<String> nodeIds,
  4. EdgeInsets padding = const EdgeInsets.all(20.0),
  5. Color color = Colors.blue,
})

Creates a group annotation that surrounds the specified nodes.

This is a convenience method that calculates the bounding box of the given nodes and creates a group that encompasses them with padding.

Parameters

  • id: Unique identifier for the group
  • title: Display title shown in the group header
  • nodeIds: Set of node IDs to surround
  • padding: Space between the group boundary and the nodes
  • color: Color for the group header and background tint

Implementation

GroupAnnotation createGroupAnnotationAroundNodes({
  required String id,
  required String title,
  required Set<String> nodeIds,
  EdgeInsets padding = const EdgeInsets.all(20.0),
  Color color = Colors.blue,
}) {
  final nodes = _parentController.nodes;
  final dependentNodes = nodeIds
      .map((nodeId) => nodes[nodeId])
      .where((node) => node != null)
      .cast<Node<T>>()
      .toList();

  Offset initialPosition = Offset.zero;
  Size initialSize = const Size(200, 150);

  if (dependentNodes.isNotEmpty) {
    // Calculate bounding box of all dependent nodes
    double minX = double.infinity;
    double minY = double.infinity;
    double maxX = double.negativeInfinity;
    double maxY = double.negativeInfinity;

    for (final node in dependentNodes) {
      final pos = node.visualPosition.value;
      final size = node.size.value;

      minX = math.min(minX, pos.dx);
      minY = math.min(minY, pos.dy);
      maxX = math.max(maxX, pos.dx + size.width);
      maxY = math.max(maxY, pos.dy + size.height);
    }

    // Add padding around the nodes
    minX -= padding.left;
    minY -= padding.top;
    maxX += padding.right;
    maxY += padding.bottom;

    initialPosition = Offset(minX, minY);
    initialSize = Size(maxX - minX, maxY - minY);
  }

  return GroupAnnotation(
    id: id,
    position: initialPosition,
    size: initialSize,
    title: title,
    color: color,
  );
}