import 'package:flutter/material.dart'; import '../domain/graph_definition.dart'; import '../layout/layout_models.dart'; import '../painting/graph_edge_layer.dart'; import '../routing/route_models.dart'; import 'graph_canvas_style.dart'; import 'graph_edge_hover.dart'; import 'graph_stage_node.dart'; /// The measured definition canvas at any viewport size and selection. /// /// Used by both the inline [GraphViewport] and the expanded presentation: the /// passive edge layer paints behind the native nodes and outcome labels, and /// every node reports selection through [onSelected]. class DefinitionGraphCanvas extends StatelessWidget { const DefinitionGraphCanvas({ super.key, required this.nodes, required this.layout, required this.routing, required this.style, required this.selectedStage, required this.onSelected, }); final List nodes; final GraphLayout layout; final GraphRouting routing; final GraphCanvasStyle style; final String? selectedStage; final ValueChanged onSelected; @override Widget build(BuildContext context) { final nodesByKey = { for (final node in nodes) node.key: node, }; final size = Size(routing.width, routing.height); return GraphEdgeHover( layout: layout, routing: routing, describe: describeGraphEdge, builder: (context, highlighted, select) => _canvas(context, nodesByKey, size, highlighted, select), ); } Widget _canvas( BuildContext context, Map nodesByKey, Size size, int? highlighted, ValueChanged select, ) { return Stack( clipBehavior: Clip.none, children: [ // The passive layer comes first so every edge paints behind the native // nodes and labels without taking their input. Positioned.fill( child: GraphEdgeLayer( routes: routing.routes, canvasSize: size, color: style.edgeColor, strokeWidth: style.edgeWidth, ), ), Positioned.fill( child: graphEdgeHighlight( context: context, layout: layout, routing: routing, highlighted: highlighted, edgeWidth: style.edgeWidth, ), ), for (final box in layout.nodes) Positioned( left: box.x, top: box.y, width: box.width, height: box.height, child: GraphStageNode( node: nodesByKey[box.key]!, selected: box.key == selectedStage, onSelected: onSelected, ), ), for (final label in routing.labels) Positioned( left: label.x, top: label.y, child: graphEdgeLabel( context: context, highlighted: label.edgeId == highlighted, description: describeGraphEdge(routing.routeFor(label.edgeId)!), onSelect: () => select(label.edgeId), child: Text( routing.routeFor(label.edgeId)!.label, style: graphEdgeLabelStyle( context, style.labelStyle, highlighted: label.edgeId == highlighted, ), ), ), ), ], ); } }