Sub topic 13
Using dynamic color on supported platforms
This chapter assumes you can build a MaterialApp with theme, darkTheme and themeMode, and that you know ColorScheme.fromSeed. It covers how to take colors from the platform, such as the Android 12+ wallpaper palette, and how to fall back safely when the platform has none.
What dynamic color is
Dynamic color is a Material 3 feature. The operating system builds a palette from the user's wallpaper (or another source it chooses). Apps read that palette and use it as their ColorScheme. Two users with different wallpapers see differently colored versions of the same app, and the app matches the rest of their device.
Flutter does not read the platform palette on its own. The dynamic_color package, published by the Flutter team, does this. It asks the platform for a light and a dark ColorScheme. Where the platform has no palette, it returns null.
| Platform | Dynamic color source | Result |
|---|---|---|
| Android 12 and newer | Wallpaper-based system palette | Light and dark schemes |
| Windows | System accent color | Schemes built from the accent |
| macOS and Linux | System accent color, where the package supports it | Schemes built from the accent |
| iOS, web, older Android | None | `null` for both schemes |
Support differs by package version and operating system release. Treat the table as a guide and check the package documentation for your version. Your code must work when the values are null, and the code below does that.
Adding the package
Add the dependency from the command line.
flutter pub add dynamic_colorThen import it where you build MaterialApp:
import 'package:dynamic_color/dynamic_color.dart';How DynamicColorBuilder works
DynamicColorBuilder is a widget. It loads the platform palette asynchronously, then calls your builder with two nullable ColorScheme values: one for light mode and one for dark mode. You return a MaterialApp built from them.
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
class DynamicApp extends StatelessWidget {
const DynamicApp({super.key});
static const _fallbackSeed = Color(0xFF6750A4);
@override
Widget build(BuildContext context) {
return DynamicColorBuilder(
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
final light = lightDynamic ??
ColorScheme.fromSeed(seedColor: _fallbackSeed);
final dark = darkDynamic ??
ColorScheme.fromSeed(
seedColor: _fallbackSeed,
brightness: Brightness.dark,
);
return MaterialApp(
theme: ThemeData(colorScheme: light),
darkTheme: ThemeData(colorScheme: dark),
home: const Scaffold(body: Center(child: Text('Dynamic color'))),
);
},
);
}
}Three points about this code:
- The
??operator picks the dynamic scheme when it exists and otherwise builds one from your seed. Your brand color is the fallback, not an afterthought. - The dark fallback passes
brightness: Brightness.dark. Without it you would get a light scheme in your dark theme. - The app still uses the default
themeModeofThemeMode.system, so the device decides which of the two schemes is shown.
Harmonizing your own colors
Dynamic schemes replace your seed color, so any fixed brand colors in your app can start to clash with them. A custom "success" green may look wrong next to a purple-tinted wallpaper palette.
The package offers a fix. Color.harmonizeWith shifts a color's hue part of the way toward another color, usually colorScheme.primary. The color stays recognizable but fits the palette.
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
Color harmonizedSuccess(BuildContext context) {
const success = Color(0xFF2E7D32);
final primary = Theme.of(context).colorScheme.primary;
return success.harmonizeWith(primary);
}Use this for colors with meaning, such as success, warning and error states. Do not harmonize a color if its exact value matters, for example a logo color you must reproduce precisely.
The package also provides ColorScheme.harmonized(), which harmonizes the scheme's error colors with its primary. You can apply it to a dynamic scheme when you want that effect:
final light = (lightDynamic ?? ColorScheme.fromSeed(seedColor: seed))
.harmonized();Combining dynamic color with ThemeExtension
If you use a ThemeExtension for custom tokens, the dynamic palette creates a problem. The extension is built once, but the scheme now changes by device. The fix is to build the extension from the scheme you actually chose.
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
@immutable
class BrandColors extends ThemeExtension<BrandColors> {
const BrandColors({required this.success, required this.onSuccess});
final Color success;
final Color onSuccess;
@override
BrandColors copyWith({Color? success, Color? onSuccess}) {
return BrandColors(
success: success ?? this.success,
onSuccess: onSuccess ?? this.onSuccess,
);
}
@override
BrandColors lerp(ThemeExtension<BrandColors>? other, double t) {
if (other is! BrandColors) return this;
return BrandColors(
success: Color.lerp(success, other.success, t)!,
onSuccess: Color.lerp(onSuccess, other.onSuccess, t)!,
);
}
}
ThemeData buildTheme(ColorScheme scheme) {
return ThemeData(
colorScheme: scheme,
extensions: [
BrandColors(
success: const Color(0xFF2E7D32).harmonizeWith(scheme.primary),
onSuccess: Colors.white,
),
],
);
}Call buildTheme(light) and buildTheme(dark) inside the DynamicColorBuilder callback. Each theme then gets tokens that match its own scheme.
Edge cases and common errors
Rebuilding the theme on every build
DynamicColorBuilder runs your builder again when the platform palette changes, for example when the user changes wallpaper while the app is open. Creating ThemeData inside the builder is fine for that reason. Do not, however, create a new DynamicColorBuilder inside a widget that rebuilds often. Keep it at the top of the app.
A flash of the fallback theme
Loading the palette is asynchronous. On the first frame, the builder can run with null values before the real scheme arrives. Users may see your seed-based theme for a moment, then a switch to the dynamic one. To reduce this, keep the first screen simple, or show a splash screen until the first real frame. Check the package documentation for how your version handles the initial load.
Dynamic colors and the user's saved preference
Dynamic color is a source of schemes. It does not replace themeMode. If you let users pick light, dark or system, that logic stays the same. You may also want a setting that turns dynamic color off, so users can choose your brand palette. That is a second persisted value: a boolean. When it is false, ignore the dynamic values:
ColorScheme resolveLight(ColorScheme? dynamicScheme, bool useDynamic, Color seed) {
if (useDynamic && dynamicScheme != null) return dynamicScheme;
return ColorScheme.fromSeed(seedColor: seed);
}Contrast is not guaranteed
The platform palette is generated from a wallpaper, and the result can have weak contrast between some roles, such as text on a container. Check key screens in both light and dark mode with several wallpapers. If text becomes hard to read, use the onX role that pairs with the surface, and never a hard-coded color.
Testing without a supported device
Most development machines and emulators will give you null, so the dynamic path is easy to leave untested. Test the builder logic by passing your own schemes. Moving the scheme choice into a pure function, as resolveLight does above, lets you unit-test it without a device.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
ColorScheme resolveLight(ColorScheme? dynamicScheme, bool useDynamic, Color seed) {
if (useDynamic && dynamicScheme != null) return dynamicScheme;
return ColorScheme.fromSeed(seedColor: seed);
}
void main() {
const seed = Color(0xFF6750A4);
test('uses the dynamic scheme when enabled and available', () {
final dynamicScheme = ColorScheme.fromSeed(seedColor: Colors.teal);
expect(resolveLight(dynamicScheme, true, seed), dynamicScheme);
});
test('falls back to the seed when the platform returns null', () {
final result = resolveLight(null, true, seed);
expect(result.primary, ColorScheme.fromSeed(seedColor: seed).primary);
});
test('ignores the dynamic scheme when the user turns it off', () {
final dynamicScheme = ColorScheme.fromSeed(seedColor: Colors.teal);
final result = resolveLight(dynamicScheme, false, seed);
expect(result.primary, ColorScheme.fromSeed(seedColor: seed).primary);
});
});Note the closing of main in the file above must be } and not });. Use this corrected ending:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
ColorScheme resolveLight(ColorScheme? dynamicScheme, bool useDynamic, Color seed) {
if (useDynamic && dynamicScheme != null) return dynamicScheme;
return ColorScheme.fromSeed(seedColor: seed);
}
void main() {
const seed = Color(0xFF6750A4);
test('uses the dynamic scheme when enabled and available', () {
final dynamicScheme = ColorScheme.fromSeed(seedColor: Colors.teal);
expect(resolveLight(dynamicScheme, true, seed), dynamicScheme);
});
test('falls back to the seed when the platform returns null', () {
final result = resolveLight(null, true, seed);
expect(result.primary, ColorScheme.fromSeed(seedColor: seed).primary);
});
test('ignores the dynamic scheme when the user turns it off', () {
final dynamicScheme = ColorScheme.fromSeed(seedColor: Colors.teal);
final result = resolveLight(dynamicScheme, false, seed);
expect(result.primary, ColorScheme.fromSeed(seedColor: seed).primary);
});
}Decision criteria
Use dynamic color when your app should feel native on Android and you have no strict brand palette. Prefer a fixed seed when brand identity matters more than matching the device, as with a banking or retail app. A good middle path is dynamic color on by default, with a setting to turn it off, and harmonized semantic colors in both cases.
Exercise
On an iPhone, DynamicColorBuilder calls your builder with lightDynamic and darkDynamic. What values do they have, and what in the code from this chapter keeps the app themed correctly?
Show answer
Both are null, because iOS provides no dynamic palette. The ?? operators then pick ColorScheme.fromSeed(seedColor: _fallbackSeed) for light and the same seed with brightness: Brightness.dark for dark, so the app uses your brand colors.
Exercise
Your app has a fixed green "success" color that looks out of place on a device with a purple dynamic palette. Which method fixes this, and what is the usual second argument?
Show answer
Call harmonizeWith on the color, for example success.harmonizeWith(Theme.of(context).colorScheme.primary). It shifts the hue toward the primary color while keeping the color recognizable.