withData<T> static method
NodeFlowViewer<T>
withData<T>({
- Key? key,
- required NodeFlowTheme theme,
- required Widget nodeBuilder(
- BuildContext context,
- Node<
T> node
- required Map<
String, Node< nodes,T> > - required List<
Connection> connections, - NodeFlowConfig? config,
- Widget nodeContainerBuilder(
- BuildContext context,
- Node<
T> node, - Widget content
- bool enablePanning = true,
- bool enableZooming = true,
- bool scrollToZoom = true,
- bool showAnnotations = false,
- bool allowSelection = false,
- ValueChanged<
Node< ? onNodeTap,T> ?> - ValueChanged<
Node< ? onNodeSelected,T> ?> - ValueChanged<
Connection?> ? onConnectionTap, - ValueChanged<
Connection?> ? onConnectionSelected, - GraphViewport? initialViewport,
Convenience factory to create a viewer with data pre-loaded.
This factory creates a NodeFlowController internally and populates it with the provided nodes and connections. Useful for quickly displaying a graph without manually managing the controller.
Parameters:
theme: Required theme for styling the viewernodeBuilder: Required function to build node contentnodes: Map of node IDs to Node objectsconnections: List of Connection objectsconfig: Optional configuration for viewport and interaction behaviorinitialViewport: Optional starting viewport position and zoom
Example:
final viewer = NodeFlowViewer.withData<String>(
theme: NodeFlowTheme.light,
nodeBuilder: (context, node) => Text(node.data),
nodes: {
'node1': Node(id: 'node1', data: 'First Node'),
'node2': Node(id: 'node2', data: 'Second Node'),
},
connections: [
Connection(
id: 'conn1',
sourceNodeId: 'node1',
targetNodeId: 'node2',
sourcePortId: 'out',
targetPortId: 'in',
),
],
);
Implementation
static NodeFlowViewer<T> withData<T>({
Key? key,
required NodeFlowTheme theme,
required Widget Function(BuildContext context, Node<T> node) nodeBuilder,
required Map<String, Node<T>> nodes,
required List<Connection> connections,
NodeFlowConfig? config,
Widget Function(BuildContext context, Node<T> node, Widget content)?
nodeContainerBuilder,
bool enablePanning = true,
bool enableZooming = true,
bool scrollToZoom = true,
bool showAnnotations = false,
bool allowSelection = false,
ValueChanged<Node<T>?>? onNodeTap,
ValueChanged<Node<T>?>? onNodeSelected,
ValueChanged<Connection?>? onConnectionTap,
ValueChanged<Connection?>? onConnectionSelected,
GraphViewport? initialViewport,
}) {
final controller = NodeFlowController<T>(
initialViewport: initialViewport,
config: config,
);
// Set theme
controller.setTheme(theme);
// Load nodes
for (final node in nodes.values) {
controller.addNode(node);
}
// Load connections
for (final connection in connections) {
controller.addConnection(connection);
}
return NodeFlowViewer<T>(
key: key,
controller: controller,
nodeBuilder: nodeBuilder,
nodeContainerBuilder: nodeContainerBuilder,
theme: theme,
enablePanning: enablePanning,
enableZooming: enableZooming,
scrollToZoom: scrollToZoom,
showAnnotations: showAnnotations,
allowSelection: allowSelection,
onNodeTap: onNodeTap,
onNodeSelected: onNodeSelected,
onConnectionTap: onConnectionTap,
onConnectionSelected: onConnectionSelected,
);
}