Design Tokens
Comprehensive guide to the Design Tokens system in the Flutter application.
Design tokens are the visual atoms of our design system. They are named entities that store visual design attributes, such as colors, typography, and spacing. This system ensures consistency across the application while allowing for high levels of customization and theming.
Core Concepts
The system is built on a layered architecture:
- Primitives: Raw values (e.g., hex codes, pixel values).
- Semantic Tokens: Tokens with specific meaning (e.g.,
primary,background,success). - Themes: Bundles of semantic tokens that define a specific look and feel (e.g.,
Modern,Retro,Cozy,Paper).
Usage in Flutter
To access design tokens in your widgets, use the designTokensProvider.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../providers/theme_provider.dart';
class MyWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final tokens = ref.watch(designTokensProvider);
return Container(
color: tokens.background,
padding: EdgeInsets.all(tokens.spacingMd),
child: Text(
'Hello World',
style: tokens.bodyPrimary.copyWith(color: tokens.primary),
),
);
}
}Theme Variants
Our system supports multiple themes, each with its own personality:
| Theme | Description | Characteristic |
|---|---|---|
| Modern | Clean and professional | Subtle rounded corners (8px), modern blue/purple palette. |
| Retro | Brutalist and bold | Sharp corners (0px), high contrast red/yellow palette. |
| Cozy | Soft and friendly | Highly rounded corners (18px/pill), warm brown/green palette. |
| Paper | Minimalist and tactile | Pure white/black backgrounds with accent-tinted surfaces. |
Dynamic Customization
The design tokens system is highly dynamic and responds to user preferences:
Accent Colors
Users can choose a custom accent color (Emerald, Blue, Indigo, etc.) which overrides the theme's default primary color. In the Paper theme, this also influences the surface and border tints.
Appearance Modes
- Light Mode: High-key color schemes.
- Dark Mode: Low-key, battery-saving color schemes.
- System: Automatically follows the device's brightness setting.
Spacing Modes
- Comfortable: Default spacing for a relaxed, airy UI.
- Compact: Approximately 70-80% of comfortable spacing, ideal for data-dense interfaces.
Typography Adjustments
- Bold Text: A global toggle that adds
FontWeight.boldto all primary and secondary text styles. - Text Scale: Adjusts the global font scale factor.
Implementation Details
The core logic resides in lib/components/utils/design_tokens.dart and lib/providers/theme_provider.dart.
DesignTokens: The data class holding all token values.AppThemeRegistry: Handles the mapping between themes/brightness and specificDesignTokensinstances.designTokensProvider: A Riverpod provider that computes the final tokens based on all active modifiers (theme, brightness, accent color, compact mode, etc.).
Adding New Tokens
To add a new token:
- Add the field to the
DesignTokensclass. - Update the constructor and
copyWithmethod. - Provide values for all theme instances (e.g.,
modernLightTokens,modernDarkTokens, etc.) indesign_tokens.dart. - If it's a spacing token, update
_applyCompactSpacinginAppThemeRegistry.