import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../ui/adaptive/host.dart'; import 'graph_viewport_controller.dart'; /// The fit control's web wording (`Fit graph to view`). const String graphFitLabel = 'Fit graph to view'; /// The expand control's web wording (`Expand graph`). const String graphExpandLabel = 'Expand graph'; /// A bounded graph canvas with the host's own pan, pinch, wheel and trackpad /// zoom, plus a fit control and an optional expanded presentation. /// /// [controller] owns the transform, so a caller can read or restore a /// [GraphViewportSnapshot]. Gestures are consumed only inside this widget's /// box: an ancestor scroll view keeps scrolling everywhere else, and a tap /// that does not move still reaches the native node under the pointer. class GraphViewport extends StatefulWidget { const GraphViewport({ super.key, required this.controller, required this.contentSize, required this.child, this.onExpand, }); final GraphViewportController controller; /// The measured graph extent; the canvas never invents a fixed size. final Size contentSize; /// The measured, already-routed graph content. final Widget child; /// Opens the expanded presentation. Null hides the expand control. final VoidCallback? onExpand; @override State createState() => _GraphViewportState(); } class _GraphViewportState extends State { Size? _scheduledViewport; Size? _scheduledContent; @override void didUpdateWidget(GraphViewport oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.controller != widget.controller) { _scheduledViewport = null; _scheduledContent = null; } } /// Attaches the current extents after layout, never during build. void _scheduleAttach(Size viewport) { if (viewport == _scheduledViewport && widget.contentSize == _scheduledContent) { return; } _scheduledViewport = viewport; _scheduledContent = widget.contentSize; WidgetsBinding.instance.addPostFrameCallback((_) { if (!mounted) return; widget.controller.attach(viewport: viewport, content: widget.contentSize); }); } @override Widget build(BuildContext context) { return LayoutBuilder( builder: (context, constraints) { final viewport = constraints.hasBoundedWidth && constraints.hasBoundedHeight ? constraints.biggest : widget.contentSize; if (!widget.controller.isAttached) { // The very first layout has no `InteractiveViewer` listening yet, so // fitting here applies before the first paint instead of flashing an // identity transform for one frame. widget.controller.attach( viewport: viewport, content: widget.contentSize, ); _scheduledViewport = viewport; _scheduledContent = widget.contentSize; } else { _scheduleAttach(viewport); } return Stack( fit: StackFit.expand, children: [ Positioned.fill( child: InteractiveViewer( key: const Key('graph-viewport'), transformationController: widget.controller.transformation, constrained: false, // The graph owns its own bounds (fit and the visible-pan clamp), // so the viewer must not impose a second, cover-the-viewport // boundary that would fight the fit scale. boundaryMargin: const EdgeInsets.all(double.infinity), minScale: widget.controller.minScale, maxScale: widget.controller.maxScale, clipBehavior: Clip.hardEdge, child: SizedBox.fromSize( size: widget.contentSize, child: widget.child, ), ), ), PositionedDirectional( top: 8, end: 8, child: Wrap( spacing: 8, children: [ _ViewportButton( buttonKey: const Key('graph-fit'), icon: _fitIcon(context), label: graphFitLabel, onPressed: widget.controller.fitToViewport, ), if (widget.onExpand != null) _ViewportButton( buttonKey: const Key('graph-expand'), icon: _expandIcon(context), label: graphExpandLabel, onPressed: widget.onExpand, ), ], ), ), ], ); }, ); } IconData _fitIcon(BuildContext context) => usesCupertinoHost(context) ? CupertinoIcons.resize : Icons.fit_screen; IconData _expandIcon(BuildContext context) => usesCupertinoHost(context) ? CupertinoIcons.fullscreen : Icons.fullscreen; } /// One native graph control: a tinted icon button on either host. /// /// The label and the button's own tap action must reach the same semantics /// node: the icon is the only excluded child, and [MergeSemantics] folds the /// host button's action into the labelled node, so a screen reader can activate /// Fit and Expand on either host. A hand-written `Semantics` sibling around an /// excluded button would announce the control with no action at all. class _ViewportButton extends StatelessWidget { const _ViewportButton({ required this.buttonKey, required this.icon, required this.label, required this.onPressed, }); final Key buttonKey; final IconData icon; final String label; final VoidCallback? onPressed; @override Widget build(BuildContext context) { return MergeSemantics( child: Semantics( button: true, label: label, child: usesCupertinoHost(context) ? CupertinoButton.tinted( key: buttonKey, padding: const EdgeInsets.all(8), onPressed: onPressed, child: ExcludeSemantics(child: Icon(icon)), ) : Tooltip( message: label, // The merged node already carries the label; the tooltip is // only the pointer affordance. excludeFromSemantics: true, child: IconButton.filledTonal( key: buttonKey, onPressed: onPressed, icon: ExcludeSemantics(child: Icon(icon)), ), ), ), ); } }