Files
pipelines/app/lib/features/templates/ui/template_detail.dart
T

216 lines
7.5 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/floating_bar_layout.dart';
import '../../../ui/adaptive/host.dart';
import '../../../ui/adaptive/segmented_bar.dart';
import '../domain/template_entry.dart';
import 'template_gallery.dart' show templatesColumn;
/// Browser wording for the gallery and the selected template, kept verbatim.
const templateSourceLabel = 'Definition source';
const templateIncludesScheduleLabel = 'Includes a schedule';
const templateCreateActionLabel = 'Create pipeline from this template';
const templateDetailKey = ValueKey<String>('template-detail');
const templateSourceKey = ValueKey<String>('template-source');
const templateCreateActionKey = ValueKey<String>('template-create-action');
const templateDetailBackKey = ValueKey<String>('template-detail-back');
const templateKeyKey = ValueKey<String>('template-key');
/// What the bar's title pill shows for an open template: its name, and its
/// description (when it has one) as the one-line subtitle.
({String title, String? subtitle}) templateBarTitle(TemplateEntry entry) {
final description = entry.description.trim();
return (
title: templateDetailName(entry),
subtitle: description.isEmpty ? null : description,
);
}
/// A gallery card's name: a blank name is the explicit unnamed placeholder.
String templateGalleryName(TemplateEntry entry) {
final name = entry.name.trim();
return name.isEmpty ? '(unnamed template)' : entry.name;
}
/// A detail's heading: a blank name falls back to the template's key.
String templateDetailName(TemplateEntry entry) {
final name = entry.name.trim();
return name.isEmpty ? entry.key : entry.name;
}
/// One template's metadata and its complete, selectable definition source.
///
/// Selecting and reading a template sends nothing. The one action opens the
/// existing create-only prompt through [onCreate]; this view never submits,
/// runs or schedules anything itself.
class TemplateDetail extends StatelessWidget {
const TemplateDetail({
super.key,
required this.entry,
required this.showBack,
required this.onBack,
required this.onCreate,
});
final TemplateEntry entry;
/// True under a shell/app bar that does not own a back control for this page.
final bool showBack;
final VoidCallback onBack;
final VoidCallback onCreate;
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
final muted = templateMutedStyle(context);
final description = entry.description.trim();
return ListView(
key: templateDetailKey,
padding: withBarClearance(context, const EdgeInsets.only(bottom: 24)),
children: [
if (showBack) _backRow(context, cupertino),
templatesColumn(
Padding(
padding: const EdgeInsets.fromLTRB(16, 8, 16, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// With a bar above, its title pill carries the name and the
// description; only a bar-less page names the template here.
if (showBack) ...[
Text(
templateDetailName(entry),
style: templateTitleStyle(context),
),
if (description.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(entry.description),
),
const SizedBox(height: 12),
],
_createButton(cupertino),
const SizedBox(height: 16),
// The key and the schedule on one quiet line, not a
// label/value document.
Wrap(
spacing: 8,
runSpacing: 4,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
SelectableText(
entry.key,
key: templateKeyKey,
semanticsLabel: 'Key ${entry.key}',
style: muted,
),
if (entry.scheduled) ...[
Text('·', style: muted),
Text(templateIncludesScheduleLabel, style: muted),
],
],
),
const SizedBox(height: 20),
Text(templateSourceLabel, style: templateSectionStyle(context)),
const SizedBox(height: 8),
// The server's source, byte-for-byte: no trim, re-indent or
// re-encoding, so selecting it copies exactly what was
// received.
SizedBox(
width: double.infinity,
child: SelectableText(
entry.source,
key: templateSourceKey,
style: templateBodyStyle(context),
),
),
],
),
),
),
],
);
}
/// A compact button at its own width: the page's one action, never a bar
/// spanning a wide window.
Widget _createButton(bool cupertino) {
if (cupertino) {
return CupertinoButton.filled(
key: templateCreateActionKey,
sizeStyle: CupertinoButtonSize.medium,
onPressed: onCreate,
child: const Text(templateCreateActionLabel),
);
}
return FilledButton.icon(
key: templateCreateActionKey,
onPressed: onCreate,
icon: const Icon(Icons.add),
label: const Text(templateCreateActionLabel),
);
}
Widget _backRow(BuildContext context, bool cupertino) {
// Only without a bar above: the same round Back the bar would show.
final back = SegmentedPill(
cupertino: cupertino,
child: SegmentedBackButton(
key: templateDetailBackKey,
cupertino: cupertino,
onPressed: onBack,
),
);
return Padding(
padding: const EdgeInsets.fromLTRB(12, 8, 16, 0),
child: Row(
children: [
back,
const SizedBox(width: 8),
Expanded(
child: Text('Templates', style: templateSectionStyle(context)),
),
],
),
);
}
}
TextStyle? templateTitleStyle(BuildContext context) =>
usesCupertinoHost(context)
? CupertinoTheme.of(context).textTheme.navLargeTitleTextStyle
: Theme.of(context).textTheme.headlineSmall;
TextStyle? templateSectionStyle(BuildContext context) =>
usesCupertinoHost(context)
? CupertinoTheme.of(context).textTheme.navTitleTextStyle
: Theme.of(context).textTheme.titleMedium;
TextStyle? templateBodyStyle(BuildContext context) {
if (usesCupertinoHost(context)) {
// SelectableText needs an explicit baseline when it inherits no style.
return CupertinoTheme.of(
context,
).textTheme.textStyle.copyWith(inherit: true);
}
return Theme.of(context).textTheme.bodyMedium;
}
TextStyle? templateMutedStyle(BuildContext context) {
if (usesCupertinoHost(context)) {
// The list rows' secondary size, not the tab bar's 10pt caption.
// SelectableText needs an explicit baseline when it inherits no style.
return CupertinoTheme.of(context).textTheme.textStyle.copyWith(
inherit: true,
fontSize: 15,
color: CupertinoColors.secondaryLabel.resolveFrom(context),
);
}
final theme = Theme.of(context);
return theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
);
}