import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../ui/adaptive/host.dart'; import '../ui/adaptive/segmented_bar.dart'; import '../ui/motion/container_transform_route.dart'; import '../ui/motion/container_transform_source.dart'; import 'destination.dart'; import 'shell_bar.dart'; /// Keeps one full-screen detail page (an open run, pipeline or chat) in step /// with a screen's selection: selecting pushes the page over the tabs, like a /// new activity, and closing it (its Back, the system Back) clears the /// selection. A selection cleared elsewhere (a route change) removes the page. class DetailPageHost { Route? _route; /// The owner's latest build of the page's content, so the open page /// follows the owner's state (a renamed title, a newer reply). final ValueNotifier _content = ValueNotifier(null); /// Whether the page is pushed (or about to be). bool get open => _route != null; void sync( BuildContext context, { required bool selected, required AppDestination destination, required WidgetBuilder builder, required VoidCallback onClosed, ContainerTransformLink? growFrom, }) { if (selected && _route != null) { // Another route cannot be rebuilt mid-build; the page follows next frame. WidgetsBinding.instance.addPostFrameCallback((_) { if (_route != null) _content.value = builder; }); } if (selected && _route == null) { _content.value = builder; final route = detailPageRoute( context, destination: destination, growFrom: growFrom, builder: (context) => ValueListenableBuilder( valueListenable: _content, builder: (context, build, _) => build == null ? const SizedBox.shrink() : build(context), ), ); _route = route; // Pushing during a build is not allowed; the page follows this frame. WidgetsBinding.instance.addPostFrameCallback((_) { if (!context.mounted || _route != route) return; Navigator.of(context).push(route); route.completed.then((_) { if (_route != route) return; _route = null; // Keep ownership until the route is actually gone. Live updates // during its exit must not push a second copy of the detail page. onClosed(); }); }); } else if (!selected && _route != null) { final route = _route!; _route = null; WidgetsBinding.instance.addPostFrameCallback((_) { if (route.isActive) route.navigator?.removeRoute(route); }); } } /// Removes the page with its owner. void dispose() { // The notifier is left to the page: a page still animating out keeps // listening to it until it is gone. final route = _route; _route = null; if (route != null && route.isActive) route.navigator?.removeRoute(route); } } /// A full-screen page whose bar shows what the page's content publishes /// through [ShellBar] (title and actions), with the platform's Back. /// /// With [growFrom] the page grows out of the list item that opened it and /// shrinks back into it ([ContainerTransformPageRoute]); otherwise it enters /// with the platform's page transition. PageRoute detailPageRoute( BuildContext context, { required AppDestination destination, required WidgetBuilder builder, ContainerTransformLink? growFrom, }) { final page = DetailPane(destination: destination, builder: builder); final cupertino = usesCupertinoHost(context); if (growFrom != null) { return ContainerTransformPageRoute( link: growFrom, cupertino: cupertino, builder: (_) => page, ); } return cupertino ? CupertinoPageRoute(builder: (_) => page) : MaterialPageRoute(builder: (_) => page); } /// An open item's own bar over its content: what the content publishes /// through [ShellBar] (title and actions). As a pushed page it implies the /// platform's Back; inline beside a list ([implyBack] false) it shows none. class DetailPane extends StatefulWidget { const DetailPane({ super.key, required this.destination, required this.builder, this.implyBack = true, }); final AppDestination destination; final WidgetBuilder builder; final bool implyBack; @override State createState() => _DetailPaneState(); } class _DetailPaneState extends State { final ShellBarRegistry _bar = ShellBarRegistry(); @override void dispose() { _bar.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final body = ShellBarScope( registry: _bar, destination: widget.destination, child: Builder(builder: widget.builder), ); return ListenableBuilder( listenable: _bar, builder: (context, _) { final spec = _bar.specFor(widget.destination); final title = spec?.title ?? ''; final actions = spec?.actions ?? const []; // Content with its own bar reaches the top edge and owns the // status-bar inset itself. The scaffold and the body's place in it stay // the same either way, so switching never remounts the content. final barless = spec?.barless ?? false; final cupertino = usesCupertinoHost(context); final bar = barless ? null : SegmentedBar( cupertino: cupertino, automaticallyImplyLeading: widget.implyBack, title: title, subtitle: spec?.subtitle, subtitleIcon: spec?.subtitleIcon, onTitlePressed: spec?.onTitlePressed, expandableTitle: spec?.expandableTitle ?? false, titleAction: spec?.titleAction, titleSemanticLabel: spec?.titleSemanticLabel, titleKey: spec?.titleKey, actions: actions, ); // Both insets pass through: the content scrolls behind the bar and // pads its lists by the bar's height (top) and by the system's or a // floating tab bar's (bottom), floating its own tabs clear of them. final content = SafeArea(top: false, bottom: false, child: body); if (cupertino) { return CupertinoPageScaffold(navigationBar: bar, child: content); } return Scaffold( extendBodyBehindAppBar: true, appBar: bar, body: content, ); }, ); } }