mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-19 19:14:33 +01:00
unified-design-system/ froze the token layer as prose -- 1220 lines of README, tokens.md and annotated HTML, and zero importable code. Every Flutter repo transcribed the token table by hand, so the "shared" palette was enforced only by discipline, and had already drifted: 10 divergent theme.dart copies (todo and untools byte-identical, the other 8 each unique), ~318 raw color literals, 4 hand-rolled AppStatusColors extensions with inconsistent field sets, ~35 inlined AlertDialogs, ~38 raw SnackBar sites with no helper anywhere. This promotes the token layer into an importable component layer, so the design system is enforced by the compiler and the package manager instead. The components own the tokens: a consumer has nothing local left to edit. Scales are the six-repo consensus, which is also what tokens.md specifies; drifted repos get corrected to this rather than the reverse. Themes are built from explicit ColorSchemes, never fromSeed -- the palette is hand-picked for contrast, and secondary/tertiary are set explicitly so SegmentedButton cannot fall back to stock Material teal. Widgets are extracted only where two or more repos already had a structurally similar implementation; SectionHeader reconciles untools' trailing-action variant with billsplit's padded one as an opt-in union, so either caller can adopt it with no visual change. 63 tests, 102/102 lines covered, gated at 100% on pre-push. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XS1m7b1piBtAL22USLW6jL
84 lines
2.7 KiB
Dart
84 lines
2.7 KiB
Dart
/// Confirmation for destructive actions.
|
|
library;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
/// Asks the user to confirm a destructive action, returning true if they do.
|
|
///
|
|
/// Exists because a delete with no confirmation and no undo is only *risky*
|
|
/// with a pointer — you have to travel to the control and press it. With a
|
|
/// keyboard it is one Tab away from a Return you were about to press anyway,
|
|
/// and the record is gone with nothing to recover it.
|
|
///
|
|
/// Defaults are chosen so the *safe* option is what a reflexive keypress hits:
|
|
/// Cancel is autofocused, and Escape dismisses. The destructive button is
|
|
/// styled with the error color but is deliberately not the default action.
|
|
///
|
|
/// Returns false when dismissed by Escape, by the barrier, or by Cancel.
|
|
Future<bool> confirmDestructive(
|
|
BuildContext context, {
|
|
required String title,
|
|
required String message,
|
|
String confirmLabel = 'Delete',
|
|
String cancelLabel = 'Cancel',
|
|
}) async {
|
|
final confirmed = await showDialog<bool>(
|
|
context: context,
|
|
builder: (dialogContext) => _ConfirmDialog(
|
|
title: title,
|
|
message: message,
|
|
confirmLabel: confirmLabel,
|
|
cancelLabel: cancelLabel,
|
|
),
|
|
);
|
|
return confirmed ?? false;
|
|
}
|
|
|
|
class _ConfirmDialog extends StatelessWidget {
|
|
const _ConfirmDialog({
|
|
required this.title,
|
|
required this.message,
|
|
required this.confirmLabel,
|
|
required this.cancelLabel,
|
|
});
|
|
|
|
final String title;
|
|
final String message;
|
|
final String confirmLabel;
|
|
final String cancelLabel;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return CallbackShortcuts(
|
|
// Escape dismisses as "no". Flutter's own barrier handles this on most
|
|
// platforms, but the desktop surface for these apps is a Chrome `--app`
|
|
// window where that cannot be assumed, and a confirmation the keyboard
|
|
// cannot back out of is worse than no confirmation at all.
|
|
bindings: {
|
|
const SingleActivator(LogicalKeyboardKey.escape): () =>
|
|
Navigator.of(context).pop(false),
|
|
},
|
|
child: AlertDialog(
|
|
title: Text(title),
|
|
content: Text(message),
|
|
actions: [
|
|
TextButton(
|
|
// Autofocused so the reflexive Return presses Cancel, not Delete.
|
|
autofocus: true,
|
|
onPressed: () => Navigator.of(context).pop(false),
|
|
child: Text(cancelLabel),
|
|
),
|
|
TextButton(
|
|
style: TextButton.styleFrom(
|
|
foregroundColor: Theme.of(context).colorScheme.error,
|
|
),
|
|
onPressed: () => Navigator.of(context).pop(true),
|
|
child: Text(confirmLabel),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|