import 'package:flutter/cupertino.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import '../../../ui/adaptive/host.dart'; import 'run_detail_tab.dart'; /// The tabs the strip renders, in the web's order: Summary first so it is the /// default, then the run Graph and the Timeline. Input and Output are /// collapsible Summary sections now, so their stable enum keys stay available /// for old routes but they are not separate tabs. const List renderedRunDetailTabs = [ RunDetailTab.summary, RunDetailTab.graph, RunDetailTab.timeline, ]; /// The run detail's platform tab strip: a Material segmented button or a /// Cupertino segmented control, in the web's Summary/Graph/Timeline order. /// /// The native control keeps every label at its intrinsic width: a narrow pane /// or scaled text scrolls the strip horizontally instead of squeezing or /// wrapping the words. A tab selected by the Graph (not tapped in the strip) /// is scrolled into view, so the operator can see which section is selected. class RunDetailTabStrip extends StatefulWidget { const RunDetailTabStrip({ super.key, required this.selected, required this.onSelected, }); final RunDetailTab selected; final ValueChanged onSelected; @override State createState() => _RunDetailTabStripState(); } class _RunDetailTabStripState extends State { final Map _labels = { for (final tab in renderedRunDetailTabs) tab: GlobalKey(), }; @override void didUpdateWidget(covariant RunDetailTabStrip oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.selected == widget.selected) return; // The Graph can select Timeline without a strip tap. Wait for the new // segment to be laid out, then reveal it inside the strip's own scroll. WidgetsBinding.instance.addPostFrameCallback((_) { if (!mounted) return; final context = _labels[widget.selected]?.currentContext; if (context == null) return; Scrollable.ensureVisible( context, alignment: 0.5, duration: const Duration(milliseconds: 200), ); }); } @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); return Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), child: ScrollWhenOverflowing( child: KeyedSubtree( key: const Key('run-detail-tabs'), child: cupertino ? _cupertinoControl() : _materialControl(), ), ), ); } Widget _materialControl() => SegmentedButton( segments: [ for (final tab in renderedRunDetailTabs) ButtonSegment(value: tab, label: _label(tab)), ], selected: {widget.selected}, showSelectedIcon: false, onSelectionChanged: (selection) => widget.onSelected(selection.single), ); Widget _cupertinoControl() => CupertinoSegmentedControl( // The strip already insets 16; the control's own default inset would put // the tabs out of line with the header and body text. padding: EdgeInsets.zero, groupValue: widget.selected, onValueChanged: widget.onSelected, children: { for (final tab in renderedRunDetailTabs) tab: Padding( padding: const EdgeInsets.symmetric(horizontal: 8), child: _label(tab), ), }, ); Widget _label(RunDetailTab tab) => KeyedSubtree( key: _labels[tab], child: KeyedSubtree( key: ValueKey('run-detail-tab-${tab.name}'), child: Text(tab.label, maxLines: 1, softWrap: false), ), ); } /// Gives [child] at least the pane width, then scrolls instead of squeezing it /// below its content width. /// /// The run detail's tab strips both use this, so the pointer-drag rule and the /// minimum-width rule live in one place. class ScrollWhenOverflowing extends StatelessWidget { const ScrollWhenOverflowing({super.key, required this.child}); final Widget child; @override Widget build(BuildContext context) { return LayoutBuilder( builder: (context, constraints) { if (!constraints.hasBoundedWidth) return child; return ScrollConfiguration( // Desktop pointer scrolling is vertical only, so a mouse can drag // the tab row as well as touch and trackpad. behavior: ScrollConfiguration.of(context).copyWith( dragDevices: const { PointerDeviceKind.touch, PointerDeviceKind.mouse, PointerDeviceKind.stylus, PointerDeviceKind.invertedStylus, PointerDeviceKind.trackpad, }, ), child: SingleChildScrollView( scrollDirection: Axis.horizontal, child: ConstrainedBox( // Content width, not the pane width: the web's tabs are compact // and left-aligned, not a full-width segmented bar. The scroll // view still reveals every segment on a narrow pane. constraints: const BoxConstraints(), child: child, ), ), ); }, ); } }