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. /// The graph is a preview on Summary that opens full screen, so it has no tab /// of its own; the run's artifacts and changes do. const List renderedRunDetailTabs = [ RunDetailTab.summary, RunDetailTab.timeline, RunDetailTab.artifacts, RunDetailTab.changes, ]; /// 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, this.tabs = renderedRunDetailTabs, this.bottom = false, }); final RunDetailTab selected; final ValueChanged onSelected; /// The tabs this run offers, in [renderedRunDetailTabs] order. final List tabs; /// The phone layout: a full-width bar under the content instead of a compact /// strip under the header. final bool bottom; @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); if (widget.bottom) { final divider = cupertino ? CupertinoColors.separator.resolveFrom(context) : Theme.of(context).colorScheme.outlineVariant; return DecoratedBox( decoration: BoxDecoration( border: Border(top: BorderSide(color: divider, width: 0.5)), ), child: Padding( padding: const EdgeInsets.fromLTRB(16, 8, 16, 8), child: SizedBox( width: double.infinity, child: KeyedSubtree( key: const Key('run-detail-tabs'), child: cupertino ? _cupertinoControl() : _materialControl(), ), ), ), ); } 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 widget.tabs) ButtonSegment(value: tab, label: _label(tab)), ], selected: {widget.selected}, showSelectedIcon: false, // The bottom bar spans the width; the header strip keeps its content size. expandedInsets: widget.bottom ? EdgeInsets.zero : null, 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 widget.tabs) 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}'), // A full-width bar shrinks a long label rather than overflowing it, // and shows the tab's glyph above its name. child: widget.bottom ? FittedBox( fit: BoxFit.scaleDown, child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon( usesCupertinoHost(context) ? tab.cupertinoIcon : tab.icon, size: 18, ), Text(tab.label, maxLines: 1, softWrap: false), ], ), ) : 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, ), ), ); }, ); } }