import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../../ui/adaptive/host.dart'; import 'schedule_pipeline_wording.dart'; /// Deletes the exact schedule revision. Returns null once the server accepted /// the delete, or the server's own reasons verbatim. typedef ScheduleDeleteSubmit = Future Function(); /// Opens the one native confirmation for a schedule deletion. /// /// Nothing is sent until `Delete schedule` is pressed; `Keep schedule` only /// closes the prompt. A refusal keeps the prompt open with the server's words. Future showScheduleDeleteDialog({ required BuildContext context, required String name, required ScheduleDeleteSubmit onSubmit, }) { final prompt = _ScheduleDeletePrompt(name: name, onSubmit: onSubmit); if (usesCupertinoHost(context)) { return showCupertinoDialog( context: context, barrierDismissible: false, builder: (_) => prompt, ); } return showDialog( context: context, barrierDismissible: false, builder: (_) => prompt, ); } class _ScheduleDeletePrompt extends StatefulWidget { const _ScheduleDeletePrompt({required this.name, required this.onSubmit}); final String name; final ScheduleDeleteSubmit onSubmit; @override State<_ScheduleDeletePrompt> createState() => _ScheduleDeletePromptState(); } class _ScheduleDeletePromptState extends State<_ScheduleDeletePrompt> { bool _sending = false; String? _refusal; void _keep() { if (!_sending) Navigator.of(context).pop(); } Future _confirm() async { if (_sending) return; setState(() { _sending = true; _refusal = null; }); final refusal = await widget.onSubmit(); if (!mounted) return; if (refusal == null) { Navigator.of(context).pop(); return; } setState(() { _sending = false; _refusal = refusal; }); } @override Widget build(BuildContext context) { final dialog = usesCupertinoHost(context) ? _cupertino(context) : _material(context); return PopScope(canPop: !_sending, child: dialog); } Widget _material(BuildContext context) { return AlertDialog( scrollable: true, title: Text(scheduleDeleteTitle(widget.name)), content: _body( errorStyle: TextStyle(color: Theme.of(context).colorScheme.error), ), actions: [ TextButton( key: scheduleDeleteKeepKey, onPressed: _sending ? null : _keep, child: const Text(scheduleDeleteKeepLabel), ), FilledButton( key: scheduleDeleteConfirmKey, onPressed: _sending ? null : _confirm, child: const Text(scheduleDeleteConfirmLabel), ), ], ); } Widget _cupertino(BuildContext context) { return CupertinoAlertDialog( title: Text(scheduleDeleteTitle(widget.name)), content: _body( errorStyle: TextStyle( color: CupertinoColors.systemRed.resolveFrom(context), ), ), actions: [ CupertinoDialogAction( key: scheduleDeleteKeepKey, onPressed: _sending ? null : _keep, child: const Text(scheduleDeleteKeepLabel), ), CupertinoDialogAction( key: scheduleDeleteConfirmKey, isDestructiveAction: true, onPressed: _sending ? null : _confirm, child: const Text(scheduleDeleteConfirmLabel), ), ], ); } Widget _body({required TextStyle errorStyle}) { return Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Text(scheduleDeleteMessage), if (_refusal case final refusal?) Padding( padding: const EdgeInsets.only(top: 8), child: Text( refusal, key: scheduleDeleteErrorKey, style: errorStyle, ), ), ], ); } }