Refactor theme implementation to use ColorScheme.fromSeed for light and dark themes; update README with usage examples and add widgetbook build configuration.
This commit is contained in:
parent
f8b33f7e0d
commit
2f84b1559f
6 changed files with 139 additions and 15 deletions
81
README.md
81
README.md
|
|
@ -33,6 +33,85 @@ flutter run
|
|||
|
||||
See `melos.yaml`, `analysis_options.yaml`, and the package-level READMEs.
|
||||
|
||||
## Using the M3E Theme
|
||||
|
||||
The design tokens and helpers live in the `m3e_design` package. Import it once in files where you access the theme:
|
||||
|
||||
```dart
|
||||
import 'package:m3e_design/m3e_design.dart';
|
||||
```
|
||||
|
||||
- Install the theme once at the app level (one-liner):
|
||||
|
||||
```dart
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
theme: ColorScheme.fromSeed(seedColor: Colors.teal).toM3EThemeData(),
|
||||
home: const MyHomePage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- With dynamic color (Android 12+), setting both light and dark themes:
|
||||
|
||||
```dart
|
||||
Widget buildDynamicApp() {
|
||||
return DynamicColorBuilder(
|
||||
builder: (lightDynamic, darkDynamic) {
|
||||
final light = lightDynamic ?? ColorScheme.fromSeed(seedColor: Colors.teal);
|
||||
final dark = darkDynamic ??
|
||||
ColorScheme.fromSeed(seedColor: Colors.teal, brightness: Brightness.dark);
|
||||
return MaterialApp(
|
||||
theme: light.toM3EThemeData(),
|
||||
darkTheme: dark.toM3EThemeData(),
|
||||
home: const MyHomePage(),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
- Access M3E tokens anywhere using the new ThemeData accessor:
|
||||
|
||||
```dart
|
||||
final m3e = Theme.of(context).m3e; // ThemeData extension
|
||||
|
||||
// Examples
|
||||
final br = m3e.shapes.round.sm; // BorderRadius
|
||||
final pad = EdgeInsets.all(m3e.spacing.md); // Spacing scale
|
||||
final bg = m3e.colors.surfaceContainerHigh; // Colors mapped to ColorScheme
|
||||
final curve = m3e.motion.emphasized; // Motion/curves
|
||||
final title = m3e.typography.base.titleLarge; // Typography
|
||||
```
|
||||
|
||||
- Apply a rounded shape to a widget:
|
||||
|
||||
```dart
|
||||
Widget roundedExample(BuildContext context) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: Theme.of(context).m3e.shapes.round.sm,
|
||||
),
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
- Optional context sugar if you prefer:
|
||||
|
||||
```dart
|
||||
// Provided by BuildContext extension in m3e_design
|
||||
final m3e = context.m3e;
|
||||
```
|
||||
|
||||
Notes:
|
||||
- In debug, accessing `Theme.of(context).m3e` asserts if the extension isn't installed, helping catch setup issues.
|
||||
- In release, it safely falls back to sensible defaults derived from the active `ColorScheme`.
|
||||
|
||||
---
|
||||
|
||||
|
|
@ -49,4 +128,4 @@ cd apps/gallery
|
|||
flutter run -d chrome
|
||||
```
|
||||
|
||||
_Last updated: 2025-10-23_
|
||||
_Last updated: 2025-10-25_
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue