import 'package:flutter/material.dart'; import '../../../ui/adaptive/floating_bar_layout.dart'; import '../../../ui/layout/app_list_avatar.dart'; import '../../../ui/layout/app_list_row.dart'; import '../domain/template_entry.dart'; import 'template_detail.dart'; import 'templates_failure.dart'; const templatesGalleryKey = ValueKey('template-gallery'); const templatesLoadingKey = ValueKey('templates-loading'); const templatesEmptyKey = ValueKey('templates-empty'); const _loadingLabel = 'Loading templates'; const _loadingMessage = 'Loading templates…'; const _emptyMessage = 'No templates are available on this server.'; Key templateRowKey(String key) => ValueKey('template-row-$key'); Key templateAvatarKey(String key) => ValueKey('template-avatar-$key'); /// The widest the gallery and an open template grow on a wide window: the /// Settings column's reading width, so the page opened from Settings lines up /// with it. const double templatesContentMaxWidth = 720; /// Caps [child] at [templatesContentMaxWidth], start-aligned under the bar. Widget templatesColumn(Widget child) => Align( alignment: AlignmentDirectional.topStart, child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: templatesContentMaxWidth), child: child, ), ); /// The gallery's truthful states and the app's shared list rows. /// /// It owns no requests: loading, failure and the actual rows all come from the /// screen, so opening a row only selects an already-read template. class TemplateGallery extends StatelessWidget { const TemplateGallery({ super.key, required this.templates, required this.stageCounts, required this.loaded, required this.loading, required this.error, required this.onRetry, required this.onOpen, }); final List templates; /// Parsed counts; a present null value means the source could not be parsed. final Map stageCounts; final bool loaded; final bool loading; final String? error; final VoidCallback onRetry; final ValueChanged onOpen; @override Widget build(BuildContext context) { if (!loaded) { if (error != null) { return TemplatesFailureState( message: error!, retrying: loading, onRetry: onRetry, ); } return _loadingState(); } if (templates.isEmpty) return _emptyState(context); return _list(context); } Widget _loadingState() { return Center( key: templatesLoadingKey, child: Semantics( liveRegion: true, label: _loadingLabel, child: Column( mainAxisSize: MainAxisSize.min, children: [ const CircularProgressIndicator(), const SizedBox(height: 12), const Text(_loadingMessage), ], ), ), ); } Widget _emptyState(BuildContext context) { return Center( key: templatesEmptyKey, child: Padding( padding: const EdgeInsets.all(24), child: Text(_emptyMessage, style: templateMutedStyle(context)), ), ); } Widget _list(BuildContext context) { // The rows scroll behind the bar and the floating tab bar. return ListView.builder( key: templatesGalleryKey, padding: withBarClearance(context, const EdgeInsets.only(bottom: 8)), itemCount: templates.length, itemBuilder: (context, index) { final entry = templates[index]; return templatesColumn( AppListRow( key: templateRowKey(entry.key), divider: index < templates.length - 1, onTap: () => onOpen(entry.key), leading: ExcludeSemantics( child: AppListAvatar.initial( templateDetailName(entry), key: templateAvatarKey(entry.key), color: _avatarColor(context, entry.key), ), ), title: Text(templateGalleryName(entry)), subtitle: _subtitle(entry), ), ); }, ); } /// One of the theme's accents, picked by the template key so a row keeps /// its colour across reloads (the Pipelines list's palette). Color _avatarColor(BuildContext context, String key) { final scheme = Theme.of(context).colorScheme; final colors = [scheme.primary, scheme.secondary, scheme.tertiary]; final hash = key.codeUnits.fold(0, (sum, unit) => sum + unit); return colors[hash % colors.length]; } /// The description, then the stage count when the server could parse the /// source. An unknown count is left out rather than announced on every row. Widget? _subtitle(TemplateEntry entry) { final count = stageCounts[entry.key]; final parts = [ if (entry.description.trim().isNotEmpty) entry.description.trim(), if (count != null) '$count ${count == 1 ? 'stage' : 'stages'}', ]; return parts.isEmpty ? null : Text(parts.join(' · ')); } }