utils/design_system/lib/src/feedback.dart
Krzysztof kuhy Rudnicki e6c9af81e9 Add design_system: the shared Flutter component layer
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
2026-08-14 22:51:06 +02:00

78 lines
2.5 KiB
Dart

/// Transient feedback (`SnackBar`) helpers.
///
/// There were ~38 raw `ScaffoldMessenger.of(context).showSnackBar(...)` call
/// sites across the fleet and zero helpers, so every one of them re-decided
/// duration, styling, and whether to clear the previous snack bar. These two
/// functions make that one decision.
library;
import 'package:design_system/src/status_colors.dart';
import 'package:design_system/src/tokens.dart';
import 'package:flutter/material.dart';
/// How long a neutral confirmation stays up.
const Duration _toastDuration = Duration(seconds: 3);
/// How long an error stays up. Longer than a toast: an error the user missed
/// is an error they will hit again.
const Duration _errorDuration = Duration(seconds: 5);
/// Shows a neutral, transient confirmation ("Saved", "Copied").
///
/// Returns null when [context] has no `ScaffoldMessenger` above it — which
/// happens routinely when an async callback completes after its screen was
/// popped, and should not throw.
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? showToast(
BuildContext context,
String message,
) {
return _show(context, message, background: null, foreground: null);
}
/// Shows a transient error, in the danger hue.
///
/// Use for something that *failed*, not for something merely noteworthy —
/// the color is the signal, so spending it on routine confirmations makes a
/// real failure invisible.
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? showError(
BuildContext context,
String message,
) {
final colors = context.statusColors;
return _show(
context,
message,
background: colors.danger,
foreground: colors.onStatus,
);
}
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? _show(
BuildContext context,
String message, {
required Color? background,
required Color? foreground,
}) {
final messenger = ScaffoldMessenger.maybeOf(context);
if (messenger == null) {
return null;
}
// Replace rather than queue: a queued snack bar describes an action the
// user has already moved on from, and shows up seconds late.
messenger.clearSnackBars();
return messenger.showSnackBar(
SnackBar(
content: Text(
message,
style: foreground == null ? null : TextStyle(color: foreground),
),
backgroundColor: background,
duration: background == null ? _toastDuration : _errorDuration,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(AppRadius.sm),
),
),
);
}