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

169 lines
5.7 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../domain/template_entry.dart';
/// 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');
/// 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);
return ListView(
key: templateDetailKey,
padding: const EdgeInsets.only(bottom: 24),
children: [
if (showBack) _backRow(context, cupertino),
Padding(
padding: const EdgeInsets.fromLTRB(16, 8, 16, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_createButton(context, cupertino),
const SizedBox(height: 12),
Text(
templateDetailName(entry),
style: templateTitleStyle(context),
),
if (entry.description.trim().isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(entry.description),
),
const SizedBox(height: 16),
Text('Key', style: templateMutedStyle(context)),
SelectableText(entry.key, style: templateBodyStyle(context)),
if (entry.scheduled)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(templateIncludesScheduleLabel),
),
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.
SelectableText(
entry.source,
key: templateSourceKey,
style: templateBodyStyle(context),
),
],
),
),
],
);
}
Widget _createButton(BuildContext context, bool cupertino) {
if (cupertino) {
return CupertinoButton.filled(
key: templateCreateActionKey,
onPressed: onCreate,
child: const Text(templateCreateActionLabel),
);
}
return FilledButton(
key: templateCreateActionKey,
onPressed: onCreate,
child: const Text(templateCreateActionLabel),
);
}
Widget _backRow(BuildContext context, bool cupertino) {
final back = cupertino
? CupertinoButton(
key: templateDetailBackKey,
padding: EdgeInsets.zero,
onPressed: onBack,
child: const Icon(CupertinoIcons.back, semanticLabel: 'Back'),
)
: BackButton(key: templateDetailBackKey, onPressed: onBack);
return Padding(
padding: const EdgeInsets.fromLTRB(4, 4, 16, 0),
child: Row(
children: [
back,
const SizedBox(width: 4),
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)) {
return CupertinoTheme.of(context).textTheme.tabLabelTextStyle.copyWith(
color: CupertinoColors.secondaryLabel.resolveFrom(context),
);
}
final theme = Theme.of(context);
return theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
);
}