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

240 lines
7.0 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import '../../../core/api_gateway.dart';
import '../data/template_repository.dart';
import '../domain/template_entry.dart';
import 'template_create_wording.dart';
import 'template_create_controls.dart';
/// Opens the native create prompt; resolves when it closes. Opening, typing and
/// Cancel send nothing; Create validates the source first and only a passing
/// validate submits it create-only. A refusal keeps the typed name; an accepted
/// `detail` is delivered through [onCreated] once.
Future<void> showTemplateCreateDialog({
required BuildContext context,
required TemplateEntry template,
required TemplateRepository repository,
required bool cupertino,
required ValueChanged<JsonMap> onCreated,
}) {
final dialog = _TemplateCreateDialog(
template: template,
repository: repository,
cupertino: cupertino,
onCreated: onCreated,
);
return cupertino
? showCupertinoDialog<void>(
context: context,
barrierDismissible: false,
builder: (_) => dialog,
)
: showDialog<void>(
context: context,
barrierDismissible: false,
builder: (_) => dialog,
);
}
class _TemplateCreateDialog extends StatefulWidget {
const _TemplateCreateDialog({
required this.template,
required this.repository,
required this.cupertino,
required this.onCreated,
});
final TemplateEntry template;
final TemplateRepository repository;
final bool cupertino;
final ValueChanged<JsonMap> onCreated;
@override
State<_TemplateCreateDialog> createState() => _TemplateCreateDialogState();
}
class _TemplateCreateDialogState extends State<_TemplateCreateDialog> {
final _name = TextEditingController();
final _feedback = GlobalKey();
bool _sending = false;
String? _error;
String? _created;
@override
void initState() {
super.initState();
_name.addListener(_onNameChanged);
}
@override
void dispose() {
_name.removeListener(_onNameChanged);
_name.dispose();
super.dispose();
}
void _onNameChanged() {
if (mounted) setState(() {});
}
bool get _fieldEnabled => !_sending && _created == null;
bool get _canSubmit => _fieldEnabled && _name.text.trim().isNotEmpty;
void _cancel() {
if (!_sending) Navigator.of(context).pop();
}
Future<void> _create() async {
if (!_canSubmit) return;
final submitted = _name.text;
setState(() {
_sending = true;
_error = null;
});
JsonMap? detail;
try {
await widget.repository.validate(widget.template.source);
detail = await widget.repository.submit(
submitted,
widget.template.source,
);
} catch (error) {
if (!mounted) return;
setState(() {
_sending = false;
_error = describeTemplateCreateError(error);
});
_revealFeedback();
return;
}
if (!mounted) return;
final created = templateCreatedName(detail, submitted);
setState(() {
_sending = false;
_created = created;
});
_revealFeedback();
widget.onCreated(detail);
}
/// The refusal/success line sits directly under the field, but a tall dialog
/// can still be scrolled away; bring it into view when it appears so a
/// refusal or an accepted create is never a silent no-op. Scrolling only when
/// it does not already fit keeps the title and field in place.
void _revealFeedback() {
WidgetsBinding.instance.addPostFrameCallback((_) {
final feedback = _feedback.currentContext;
if (feedback == null) return;
final box = feedback.findRenderObject();
final scrollable = Scrollable.maybeOf(feedback);
final viewport = RenderAbstractViewport.maybeOf(box);
if (box is! RenderBox || !box.attached || scrollable == null) return;
if (viewport == null) return;
final position = scrollable.position;
final top = viewport.getOffsetToReveal(box, 0).offset;
final start = position.pixels;
final end = start + position.viewportDimension;
if (top >= start - 1 && top + box.size.height <= end + 1) return;
final target = top < start
? top
: viewport.getOffsetToReveal(box, 1).offset;
position.jumpTo(
target
.clamp(position.minScrollExtent, position.maxScrollExtent)
.toDouble(),
);
});
}
@override
Widget build(BuildContext context) {
final color = widget.cupertino
? CupertinoColors.systemRed.resolveFrom(context)
: Theme.of(context).colorScheme.error;
final content = Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TemplateCreateNameField(
cupertino: widget.cupertino,
controller: _name,
enabled: _fieldEnabled,
onSubmit: _create,
),
// Feedback stays next to the field it belongs to. The note below can
// be pushed out of the dialog's scroll viewport on a short, large-text
// screen; a refusal or success line must not be.
Column(
key: _feedback,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (_error case final error?)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
error,
key: templateCreateErrorKey,
style: TextStyle(color: color),
),
),
if (_created case final created?)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
templateCreateSuccess(created),
key: templateCreateSuccessKey,
),
),
],
),
const SizedBox(height: 12),
const Text(templateCreateNote),
],
);
final actions = [
templateCreateDialogAction(
cupertino: widget.cupertino,
key: templateCreateCancelKey,
onPressed: _sending ? null : _cancel,
child: Text(
_created == null
? templateCreateCancelLabel
: templateCreateCloseLabel,
),
),
templateCreateDialogAction(
cupertino: widget.cupertino,
key: templateCreateConfirmKey,
primary: true,
onPressed: _canSubmit ? _create : null,
child: Text(
_sending ? templateCreateSendingLabel : templateCreateConfirmLabel,
),
),
];
final title = Text(templateCreateTitle(widget.template));
if (!widget.cupertino) {
return PopScope(
canPop: !_sending,
child: AlertDialog(
scrollable: true,
title: title,
content: content,
actions: actions,
),
);
}
return PopScope(
canPop: !_sending,
child: CupertinoAlertDialog(
title: title,
content: content,
actions: actions,
),
);
}
}