import '../ui/motion/motion.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../ui/adaptive/segmented_bar.dart'; import '../ui/adaptive/floating_bar_layout.dart'; import '../ui/adaptive/floating_destination_bar.dart'; import 'attention_badge.dart'; import 'destination.dart'; import 'list_detail.dart'; import 'shell_bar.dart'; class AdaptiveShell extends StatefulWidget { const AdaptiveShell({ super.key, this.destinationBodies = const {}, this.cupertino = false, this.destination, this.onDestinationSelected, this.onSearch, this.attentionCount = 0, }); final Map destinationBodies; final bool cupertino; /// The destination a route owner selected. When null the shell keeps its own /// selection, which preserves the plain-shell tests and native composition. final AppDestination? destination; final ValueChanged? onDestinationSelected; /// Opens the transient Search page. Null hides the action, so the plain /// shell tests and compositions without routing keep their tab set only. final VoidCallback? onSearch; /// Runs needing the operator today, mirrored on the Runs tab in every mount /// so it stays visible from any destination. Zero shows no badge. final int attentionCount; @override State createState() => _AdaptiveShellState(); } class _AdaptiveShellState extends State { late AppDestination _selected; final Set _visited = {}; final ShellBarRegistry _bar = ShellBarRegistry(); // Keeps every destination's state if the stack's parent ever changes. final GlobalKey _destinationStackKey = GlobalKey(); @override void dispose() { _bar.dispose(); super.dispose(); } @override void initState() { super.initState(); _selected = widget.destination ?? AppDestination.pipelines; _visited.add(_selected); } @override void didUpdateWidget(covariant AdaptiveShell oldWidget) { super.didUpdateWidget(oldWidget); final destination = widget.destination; if (destination != null) { _selected = destination; _visited.add(destination); } } void _select(AppDestination destination) { final callback = widget.onDestinationSelected; if (callback != null) { callback(destination); return; } setState(() { _selected = destination; _visited.add(destination); }); } /// The destination's icon; only Runs carries the attention count. Widget _icon(AppDestination destination, IconData data) { Widget icon = Icon(data); if (destination == AppDestination.chat) { return ActivityBadge( key: const ValueKey('shell.badge.chat.unread'), count: _bar.badge(destination, ShellBadgeKind.unread), label: 'unread chats', cupertino: widget.cupertino, child: icon, ); } if (destination != AppDestination.runs) return icon; // One indicator: runs that need you outrank runs in progress, so the // tab never wears two counts at once. final attention = widget.attentionCount > 0; return AttentionDestinationIcon( count: widget.attentionCount, cupertino: widget.cupertino, child: ActivityBadge( key: const ValueKey('shell.badge.runs.running'), count: attention ? 0 : _bar.badge(destination, ShellBadgeKind.running), label: 'runs in progress', cupertino: widget.cupertino, child: icon, ), ); } @override Widget build(BuildContext context) { // Full-screen detail routes cover the shell themselves. Keep the root tabs // mounted/layout-ready underneath, so Back does not resize or reveal them // only after the outgoing page has finished its native animation. final index = AppDestination.values.indexOf(_selected); final details = destinationDetails(_selected); final contents = AppDestination.values.map((destination) { final item = destinationDetails(destination); return KeyedSubtree( key: ValueKey(destination), child: ShellBarScope( registry: _bar, destination: destination, child: _visited.contains(destination) ? widget.destinationBodies[destination]?.call(context) ?? Center(child: Text('${item.label} — Not implemented')) : const SizedBox.shrink(), ), ); }).toList(); return ListenableBuilder( listenable: _bar, builder: (context, _) { final spec = _bar.specFor(_selected); // One floating glass tab bar at every width: full width on a phone, // sized to its tabs at the right edge on a wide window (the bar judges // that itself, live). Content keeps the whole width and scrolls behind // it, learning the bar's height as its bottom media padding. Neither // scaffold's structure depends on the width, so resizing never // remounts a destination. // // While the tab shows its list beside an open item, the bar floats // over the list pane only (judging its placement on that width), so // it never covers the item or stacks on the item's own composer. final split = _bar.split(_selected); final bar = ListPaneWidth( split: split, alignment: AlignmentDirectional.bottomStart, child: FloatingDestinationBar( cupertino: widget.cupertino, currentIndex: index, onTap: (value) => _select(AppDestination.values[value]), items: _destinationItems(), ), ); // Destinations reach the top edge and scroll behind the floating top // bar too: each learns the bar's height as its top media padding and // pads its own lists by it (see withTopBarClearance). final topBar = _ListPaneBar( split: split, bar: _segmentedBar(spec, details.label), ); // The scaffolds below swap the system insets for their bars' // heights; panes beside a list read the real ones from here. final system = MediaQuery.paddingOf(context); if (widget.cupertino) { return ShellSystemInsets( padding: system, child: CupertinoPageScaffold( navigationBar: topBar, child: SafeArea( top: false, bottom: false, child: FloatingBarOverlay( bar: bar, child: _destinationStack(contents, index), ), ), ), ); } return ShellSystemInsets( padding: system, child: Scaffold( extendBodyBehindAppBar: true, appBar: topBar, body: _destinationStack(contents, index), extendBody: true, bottomNavigationBar: bar, ), ); }, ); } List _destinationItems() => AppDestination.values.map((destination) { final item = destinationDetails(destination); return FloatingDestinationItem( key: ValueKey('shell.destination.${destination.name}'), icon: _icon( destination, widget.cupertino ? item.cupertinoIcon : item.icon, ), label: item.label, ); }).toList(); /// The open screen's own bar: its Back, title and actions, or the tab's /// name with Search. SegmentedBar _segmentedBar(ShellBarSpec? spec, String label) { final cupertino = widget.cupertino; final onBack = spec?.onBack; final published = spec?.actions ?? const []; final onSearch = widget.onSearch; return SegmentedBar( cupertino: cupertino, automaticallyImplyLeading: false, leading: onBack == null ? null : SegmentedBackButton( key: const ValueKey('shell.back'), cupertino: cupertino, onPressed: onBack, ), title: spec?.title ?? label, titleWidget: spec?.titleWidget, titleWidgetMaxWidth: spec?.titleWidgetMaxWidth ?? double.infinity, backgroundColor: spec?.backgroundColor, subtitle: spec?.subtitle, subtitleIcon: spec?.subtitleIcon, onTitlePressed: spec?.onTitlePressed, titleSemanticLabel: spec?.titleSemanticLabel, titleKey: spec?.titleKey, actions: [ if (published.isNotEmpty) // A screen taking the bar over cross-fades its actions in. MotionSwitcher( child: Row( key: ObjectKey(_bar.ownerFor(_selected)), mainAxisSize: MainAxisSize.min, children: [ for (final action in published) Flexible(child: action), ], ), ) // A screen whose main pill is its own field (Search) needs no // separate Search action. else if (onSearch != null && spec?.titleWidget == null) cupertino ? CupertinoButton( key: const ValueKey('shell.search'), padding: EdgeInsets.zero, onPressed: onSearch, child: const Icon( CupertinoIcons.search, semanticLabel: 'Search', ), ) : IconButton( key: const ValueKey('shell.search'), onPressed: onSearch, tooltip: 'Search', icon: const Icon(Icons.search), ), ], ); } Widget _destinationStack(List contents, int index) => IndexedStack(key: _destinationStackKey, index: index, children: contents); } /// The shell's top bar, over the list pane only while the tab shows a list /// beside an open item ([ShellPaneSplit]); the item's own bar takes the rest /// of the same row. The bar keeps its place in the scaffold either way, so a /// split never remounts it. class _ListPaneBar extends StatelessWidget implements ObstructingPreferredSizeWidget { const _ListPaneBar({required this.split, required this.bar}); final bool split; final SegmentedBar bar; @override Size get preferredSize => bar.preferredSize; @override bool shouldFullyObstruct(BuildContext context) => bar.shouldFullyObstruct(context); @override Widget build(BuildContext context) => ListPaneWidth( split: split, alignment: AlignmentDirectional.topStart, child: bar, ); }