utils/design_system/lib/src/theme.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

106 lines
3.9 KiB
Dart

/// The shared `ThemeData` builders.
///
/// Built from explicit `ColorScheme`s, never `ColorScheme.fromSeed`: the
/// shared palette is hand-picked to hit specific contrast ratios, and a seeded
/// scheme silently replaces those values with algorithmically derived ones.
library;
import 'package:design_system/src/status_colors.dart';
import 'package:design_system/src/tokens.dart';
import 'package:flutter/material.dart';
/// Builds the shared light [ThemeData].
ThemeData buildLightTheme() {
const colorScheme = ColorScheme.light(
surface: AppPalette.paper,
surfaceContainerHighest: AppPalette.paperRaised,
onSurface: AppPalette.textOnLight,
onSurfaceVariant: AppPalette.mutedOnLight,
outline: AppPalette.lineLight,
primary: AppPalette.accent,
// Filled surfaces use dark text: the accent is light enough that white
// on it fails contrast.
onPrimary: AppPalette.onFill,
// secondary/tertiary — the shared palette has one accent, not a
// separate secondary hue, so these mirror primary. Without an explicit
// value here, widgets that reach for secondaryContainer (e.g.
// SegmentedButton's selected segment) silently fall back to Flutter's
// stock Material teal — confirmed live on-device, not just in theory.
secondary: AppPalette.accent,
onSecondary: AppPalette.onFill,
secondaryContainer: AppPalette.lineLight,
onSecondaryContainer: AppPalette.accent,
tertiary: AppPalette.accent,
onTertiary: AppPalette.onFill,
tertiaryContainer: AppPalette.lineLight,
onTertiaryContainer: AppPalette.accent,
error: AppPalette.danger,
onError: AppPalette.onFill,
);
return _buildTheme(colorScheme);
}
/// Builds the shared dark [ThemeData].
ThemeData buildDarkTheme() {
const colorScheme = ColorScheme.dark(
surface: AppPalette.ink,
surfaceContainerHighest: AppPalette.inkRaised2,
surfaceContainerHigh: AppPalette.inkRaised1,
onSurface: AppPalette.textOnDark,
onSurfaceVariant: AppPalette.mutedOnDark,
outline: AppPalette.lineDark,
primary: AppPalette.accent,
onPrimary: AppPalette.onFill,
secondary: AppPalette.accent,
onSecondary: AppPalette.onFill,
secondaryContainer: AppPalette.lineDark,
onSecondaryContainer: AppPalette.accent,
tertiary: AppPalette.accent,
onTertiary: AppPalette.onFill,
tertiaryContainer: AppPalette.lineDark,
onTertiaryContainer: AppPalette.accent,
error: AppPalette.danger,
onError: AppPalette.onFill,
);
return _buildTheme(colorScheme);
}
ThemeData _buildTheme(ColorScheme colorScheme) {
return ThemeData(
useMaterial3: true,
colorScheme: colorScheme,
scaffoldBackgroundColor: colorScheme.surface,
extensions: const [AppStatusColors.standard()],
textTheme: const TextTheme(
bodyLarge: TextStyle(fontSize: AppTextSize.body),
bodyMedium: TextStyle(fontSize: AppTextSize.body),
titleLarge: TextStyle(fontSize: AppTextSize.title),
titleMedium: TextStyle(fontSize: AppTextSize.subtitle),
labelMedium: TextStyle(fontSize: AppTextSize.label),
labelSmall: TextStyle(fontSize: AppTextSize.caption),
),
inputDecorationTheme: InputDecorationTheme(
filled: true,
fillColor: colorScheme.surfaceContainerHighest,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(AppRadius.sm),
borderSide: BorderSide(color: colorScheme.outline),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(AppRadius.sm),
borderSide: BorderSide(color: colorScheme.outline),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(AppRadius.sm),
borderSide: BorderSide(color: colorScheme.primary, width: 2),
),
labelStyle: TextStyle(color: colorScheme.onSurfaceVariant),
),
dividerTheme: DividerThemeData(
color: colorScheme.outline,
thickness: 1,
space: AppSpacing.md,
),
);
}