Skip to content

Commit

Permalink
add section for dynamic themes
Browse files Browse the repository at this point in the history
  • Loading branch information
tilucasoli authored Dec 3, 2024
1 parent ec7e98d commit 5264d39
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions website/pages/docs/guides/design-token.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,66 @@ MixTheme(

`MixThemeData` is a class that defines the values for the design tokens that will be used throughout the application. It has five properties related to Design Tokens: `colors`, `textStyles`, `spaces`, `radii`, and `breakpoints`. Each property is a map that contains the tokens and their values.

```dart {1-4}
const primary = ColorToken('primary');
const h1 = TextStyleToken('h1');
const radiusMedium = RadiusToken('radiusMedium');
const spaceMedium = SpaceToken('spaceMedium');
final docTheme = MixThemeData(
colors: {
primary: Colors.lightBlue,
},
textStyles: {
h1: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
},
radii: {
radiusMedium: const Radius.circular(10),
},
spaces: {
spaceMedium: 10,
},
);
```

Also, `MixThemeData` has an additional property called `defaultOrderOfModifiers`, which is used to override the default order of modifiers. Therefore, if you prefer to use a different order of modifiers, you can set it in the `defaultOrderOfModifiers` instead of using the `orderOfModifiers` property in each `StyledWidget`. To set your own order of modifiers, you just need to pass a list of types that extends `WidgetModifierSpec`. For instance:

```dart {3-5}
final overrideOrderTheme = MixThemeData(
defaultOrderOfModifiers: [
```dart
final docTheme = MixThemeData(
// ...
defaultOrderOfModifiers: const [
SizedBoxModifierSpec,
ClipRectModifierSpec,
TransformModifierSpec,
],
);
```

### Dynamic Themes

Mix allows you to define dynamic themes, which means that the values of the design tokens can be resolved dynamically using a [BuildContext](https://api.flutter.dev/flutter/widgets/BuildContext-class.html). To do this, you need to use the `ColorResolver`, `TextStyleResolver`, `RadiusResolver`, `SpaceResolver` and `BreakpointResolver` classes. These classes are used to resolve the values of the design tokens based on the current `BuildContext`.

For example:

```dart
final surface = ColorToken('surface');
final docTheme = MixThemeData(
colors: {
surface: ColorResolver((context) {
if (MediaQuery.of(context).platformBrightness == Brightness.dark) {
return Colors.black;
}
return Colors.white;
}),
},
);
```

## Applying design tokens

After defining the design tokens, you can use them in your application to style your widgets. All attributes that accept a token as a value have a `ref` method that allows you to access the token value. For instance:
Expand Down

0 comments on commit 5264d39

Please sign in to comment.