364fd5b538
Keep the Material-only run action behavior while preserving public Cupertino parameters for callers that will be cleaned up separately. Narrow the related tests to the supported Material path.
121 lines
3.3 KiB
Dart
121 lines
3.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../domain/run_deletion.dart';
|
|
|
|
const deleteRunsConfirmKey = ValueKey<String>('runs.delete.confirm');
|
|
const deleteRunsKeepKey = ValueKey<String>('runs.delete.keep');
|
|
const deleteRunsErrorKey = ValueKey<String>('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<String?> 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<bool> showDeleteRunsPrompt({
|
|
required BuildContext context,
|
|
required int count,
|
|
required String? size,
|
|
required bool cupertino,
|
|
required DeleteRunsSubmit onSubmit,
|
|
}) async {
|
|
final prompt = _DeleteRunsPrompt(count: count, size: size, onSubmit: onSubmit);
|
|
final result = await showDialog<bool>(
|
|
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<void> _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),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|