GraphiteCanvas is the primary widget for rendering a graph composed of nodes and edges. It manages the layout of nodes within a Matrix, handles edge drawing, and provides extensive gesture support for both nodes and edges.
Key Configuration Properties
Layout & Scaling
matrix: The Matrix object defining the graph structure.defaultCellSize: The base Size for cells.cellPadding: EdgeInsets applied to cells.orientation: MatrixOrientation (Horizontal or Vertical).minScale / maxScale: Zoom constraints.contactEdgesDistance: Distance used for edge connection points.
Customization Builders
builder: A NodeCellBuilder to define how individual nodes are rendered.overlayBuilder: An OverlayBuilder to render custom widgets on top of the graph (e.g., tooltips or selection highlights).styleBuilder: An EdgeStyleBuilder to customize the visual appearance of edges.pathBuilder: An EdgePathBuilder to customize the geometric path of edges.edgeLabels: Configuration for displaying text labels on edges.
Gesture Callbacks
GraphiteCanvas supports a wide range of gestures for nodes, edges, and the canvas background, including:
- Tap:
onNodeTapDown, onNodeTapUp, onEdgeTapDown, onEdgeTapUp, onCanvasTap. - Secondary Tap:
onNodeSecondaryTapDown, onNodeSecondaryTapUp, onEdgeSecondaryTapDown, onEdgeSecondaryTapUp. - Long Press:
onNodeLongPressStart, onNodeLongPressEnd, onNodeLongPressMoveUpdate, onEdgeLongPressStart, etc. - Force Press:
onNodeForcePressStart, onNodeForcePressPeak, onEdgeForcePressPeak, etc. - Pan:
onNodePanStart, onNodePanUpdate, onNodePanDown.
Implementation Example
GraphiteCanvas(
matrix: myMatrix,
defaultCellSize: Size(100, 50),
cellPadding: EdgeInsets.all(8.0),
contactEdgesDistance: 5.0,
orientation: MatrixOrientation.Horizontal,
maxScale: 3.0,
minScale: 0.5,
clipBehavior: Clip.none,
builder: (context, node) {
return MyCustomNodeWidget(node: node);
},
onNodeTapDown: (node) {
print('Tapped node: ${node.id}');
},
onCanvasTap: () {
print('Canvas tapped');
},
);