import 'package:flutter/material.dart'; import '../domain/run_deletion.dart'; const deleteRunsConfirmKey = ValueKey('runs.delete.confirm'); const deleteRunsKeepKey = ValueKey('runs.delete.keep'); const deleteRunsErrorKey = ValueKey('runs.delete.error'); /// Sends the confirmed deletion. Returns null once the server replied, or the /// failure to show in the still-open prompt. typedef DeleteRunsSubmit = Future Function(); /// Opens the native destructive confirmation for deleting [count] runs. /// /// Nothing is sent until Delete is pressed; Cancel only closes the prompt. /// Resolves true when the server answered the deletion. Future showDeleteRunsPrompt({ required BuildContext context, required int count, required String? size, required DeleteRunsSubmit onSubmit, }) async { final prompt = _DeleteRunsPrompt( count: count, size: size, onSubmit: onSubmit, ); final result = await showDialog( context: context, barrierDismissible: false, builder: (_) => prompt, ); return result ?? false; } class _DeleteRunsPrompt extends StatefulWidget { const _DeleteRunsPrompt({ required this.count, required this.size, required this.onSubmit, }); final int count; final String? size; final DeleteRunsSubmit onSubmit; @override State<_DeleteRunsPrompt> createState() => _DeleteRunsPromptState(); } class _DeleteRunsPromptState extends State<_DeleteRunsPrompt> { bool _sending = false; String? _error; Future _confirm() async { if (_sending) return; setState(() { _sending = true; _error = null; }); final error = await widget.onSubmit(); if (!mounted) return; if (error == null) { Navigator.of(context).pop(true); return; } setState(() { _sending = false; _error = error; }); } void _keep() { if (!_sending) Navigator.of(context).pop(false); } @override Widget build(BuildContext context) => PopScope(canPop: !_sending, child: _material(context)); Widget _material(BuildContext context) { final scheme = Theme.of(context).colorScheme; return AlertDialog( scrollable: true, title: Text(deleteRunsTitle(widget.count)), content: _body(TextStyle(color: scheme.error)), actions: [ TextButton( key: deleteRunsKeepKey, onPressed: _sending ? null : _keep, child: const Text('Cancel'), ), FilledButton( key: deleteRunsConfirmKey, style: FilledButton.styleFrom( backgroundColor: scheme.error, foregroundColor: scheme.onError, ), onPressed: _sending ? null : _confirm, child: Text(_sending ? 'Deleting…' : 'Delete'), ), ], ); } Widget _body(TextStyle errorStyle) => Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text(deleteRunsMessage(widget.count, size: widget.size)), if (_error case final error?) Padding( padding: const EdgeInsets.only(top: 8), child: Semantics( liveRegion: true, child: Text(error, key: deleteRunsErrorKey, style: errorStyle), ), ), ], ); }