Custom ThemeExtension for Brand Tokens
Define and consume bespoke design tokens by authoring a ThemeExtension class.
Why Brand Tokens Need a Home
Material 3 gives you a rich ColorScheme, but real products carry extra design decisions that don't map to any built-in slot: a brand gradient, a success color, a promotional accent, a custom card radius.
Hardcoding these as global constants breaks down the moment you support light and dark modes, because a single constant can't change with the active ThemeData.
- You want these values to live inside the theme.
- You want them to interpolate smoothly during theme animations.
- You want to read them with the same
Theme.of(context)ergonomics as everything else.
Flutter's answer is ThemeExtension.
What a ThemeExtension Is
ThemeExtension<T> is an abstract class you subclass to attach your own typed bundle of values to a ThemeData.
The generic parameter T is your own class. This is what lets Flutter store and later retrieve your extension by its exact type instead of by a string key.
- It is type-safe: no casting from a
Map. - It is theme-aware: it ships inside
ThemeData, so light and dark can hold different instances. - It supports animation: Flutter calls
lerpto blend two instances when themes change.
You must implement two methods: copyWith and lerp.
All lessons in this course
- Material 3 Color Schemes and Seed Colors
- Dynamic Color and Adaptive Light/Dark Themes
- Custom ThemeExtension for Brand Tokens
- Responsive Typography and Component Theming