import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../ui/adaptive/segmented_bar.dart'; import '../core/api_gateway.dart'; import '../features/templates/screen.dart'; import '../shell/destination.dart'; import '../shell/shell_bar.dart'; import '../ui/adaptive/host.dart'; import '../ui/adaptive/floating_bar_layout.dart'; import '../ui/adaptive/top_bar_notices.dart'; import 'app_session.dart'; import 'connection_status.dart'; import 'server_endpoint.dart'; /// The Back control of the app-level Templates page. const templatesPageBackKey = ValueKey('templates-page-back'); /// The Templates destination: the landed gallery over the app's shared gateway. /// /// The gallery is rendered in embedded mode, so the shell owns navigation; this /// destination contributes the page's one native bar (Back + Templates) and the /// same honest connection states as the other destinations. No socket is opened /// here: [session] owns the only connection, and a template read, validate or /// create request travels over it. /// /// [onCreated] forwards the server-confirmed detail once, so the caller can /// invalidate the Pipelines list. This screen never inserts a row itself. class TemplatesDestination extends StatelessWidget { const TemplatesDestination({ super.key, required this.session, required this.server, required this.onBack, this.onCreated, this.initialKey, }); final AppSession session; final ServerEndpoint server; /// Closes this page and restores the Settings destination underneath. final VoidCallback onBack; final ValueChanged? onCreated; /// The template detail the route names, or null for the gallery. final String? initialKey; @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); return AnimatedBuilder( animation: session, builder: (context, _) => _TemplatesPage( cupertino: cupertino, onBack: onBack, child: _body(cupertino), ), ); } Widget _body(bool cupertino) { if (!session.everConnected) { return BelowTopBar( child: ConnectionGate( session: session, server: server, cupertino: cupertino, ), ); } return TopBarNotices( notices: [ if (!session.connected) ConnectionBanner(session: session, cupertino: cupertino), ], child: TemplatesScreen( gateway: session.gateway, embedded: true, onCreated: onCreated, initialKey: initialKey, ), ); } } /// The page's standard native bar. Back is named explicitly so it cannot be /// confused with the gallery's own Back-to-gallery control. The gallery puts /// Refresh in this bar ([ShellBar]); its content scrolls behind the bar. class _TemplatesPage extends StatefulWidget { const _TemplatesPage({ required this.cupertino, required this.onBack, required this.child, }); final bool cupertino; final VoidCallback onBack; final Widget child; @override State<_TemplatesPage> createState() => _TemplatesPageState(); } class _TemplatesPageState extends State<_TemplatesPage> { final ShellBarRegistry _bar = ShellBarRegistry(); @override void dispose() { _bar.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final cupertino = widget.cupertino; // The page reaches its top edge; the gallery pads its lists by the bar. final body = SafeArea( top: false, bottom: false, child: ShellBarScope( registry: _bar, destination: AppDestination.settings, child: widget.child, ), ); return ListenableBuilder( listenable: _bar, builder: (context, _) { final actions = _bar.specFor(AppDestination.settings)?.actions ?? const []; if (cupertino) { return CupertinoPageScaffold( navigationBar: SegmentedBar( cupertino: true, leading: CupertinoButton( key: templatesPageBackKey, padding: EdgeInsets.zero, onPressed: widget.onBack, child: const Icon( CupertinoIcons.back, semanticLabel: 'Back to Settings', ), ), title: 'Templates', actions: actions, ), child: body, ); } return Scaffold( extendBodyBehindAppBar: true, appBar: SegmentedBar( cupertino: false, leading: IconButton( key: templatesPageBackKey, tooltip: 'Back to Settings', onPressed: widget.onBack, icon: const Icon(Icons.arrow_back), ), title: 'Templates', actions: actions, ), body: body, ); }, ); } }