import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../ui/adaptive/host.dart'; import 'graph_viewport.dart'; import 'graph_viewport_controller.dart'; /// The expanded presentation's close wording (`Close`). const String graphExpandedClose = 'Close'; /// Builds the same canvas for another viewport, with the selection that /// presentation is showing. typedef GraphExpandedCanvasBuilder = Widget Function( BuildContext context, String? selectedStage, ValueChanged onSelected, ); /// A native full-screen presentation of one graph canvas. /// /// It fits the graph into the larger box (as the web does when its panel /// expands), keeps the selected node selected, and lets a tap in the larger /// canvas update that selection through [onSelected]. Closing pops the route; /// the inline canvas was never changed, so it returns to its previous state. class GraphExpandedView extends StatefulWidget { const GraphExpandedView({ super.key, required this.contentSize, required this.canvasBuilder, required this.title, required this.onSelected, this.selectedStage, this.header, }); /// The measured graph extent, as in the inline canvas. final Size contentSize; final GraphExpandedCanvasBuilder canvasBuilder; /// The presentation's native title, e.g. `Run graph`. final String title; /// Reports a node activated in the expanded canvas. final ValueChanged onSelected; /// Optional fixed content above the canvas, e.g. a legend that explains the /// graph's marks. It stays outside the pan/zoom transform. final Widget? header; final String? selectedStage; @override State createState() => _GraphExpandedViewState(); } class _GraphExpandedViewState extends State { final GraphViewportController _controller = GraphViewportController(); late String? _selected = widget.selectedStage; @override void didUpdateWidget(GraphExpandedView oldWidget) { super.didUpdateWidget(oldWidget); if (widget.selectedStage != oldWidget.selectedStage) { _selected = widget.selectedStage; } } @override void dispose() { _controller.dispose(); super.dispose(); } void _select(String key) { setState(() => _selected = key); widget.onSelected(key); } void _close() => Navigator.of(context).maybePop(); @override Widget build(BuildContext context) { final viewport = GraphViewport( key: const Key('graph-expanded-viewport'), controller: _controller, contentSize: widget.contentSize, // The larger box fits the whole graph, as the web does when its panel // expands; a later resize re-fits until the reader pans or zooms, then // only clamps. child: widget.canvasBuilder(context, _selected, _select), ); final body = widget.header == null ? viewport : Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ widget.header!, Expanded(child: viewport), ], ); if (usesCupertinoHost(context)) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( automaticallyImplyLeading: false, middle: Text(widget.title), trailing: CupertinoButton( key: const Key('graph-expanded-close'), padding: EdgeInsets.zero, onPressed: _close, child: const Text(graphExpandedClose), ), ), child: SafeArea(child: body), ); } return Scaffold( appBar: AppBar( title: Text(widget.title), automaticallyImplyLeading: false, actions: [ TextButton( key: const Key('graph-expanded-close'), onPressed: _close, child: const Text(graphExpandedClose), ), ], ), body: SafeArea(child: body), ); } } /// Pushes [GraphExpandedView] with the host's native full-screen transition. /// /// This is the seam a Graph tab or run detail can call: pass the measured /// content size and a builder that draws the same canvas for the supplied /// selection. The expanded canvas fits that size; the inline canvas is /// untouched. Future openGraphExpanded( BuildContext context, { required Size contentSize, required GraphExpandedCanvasBuilder canvasBuilder, required String title, required ValueChanged onSelected, String? selectedStage, Widget? header, }) { final page = GraphExpandedView( contentSize: contentSize, canvasBuilder: canvasBuilder, title: title, onSelected: onSelected, selectedStage: selectedStage, header: header, ); final route = usesCupertinoHost(context) ? CupertinoPageRoute(fullscreenDialog: true, builder: (_) => page) : MaterialPageRoute(fullscreenDialog: true, builder: (_) => page); return Navigator.of(context).push(route); }