Sub topic 13
Using Dynamic Color on Supported Platforms
Assume you know Dart null safety, widget trees, MaterialApp, Scaffold, and BuildContext. This chapter shows how to use platform-provided colors while keeping a predictable fallback for platforms that do not support dynamic color.
Dynamic color means that an operating system supplies a color palette based on the user's wallpaper or system settings. Android 12 and later can provide these palettes. Flutter receives the palette through the dynamic_color package. Your app can use the supplied light and dark ColorScheme objects instead of generating both schemes from one fixed seed color.
Add the package
From the root of your Flutter project, run:
flutter pub add dynamic_colorThe package exposes DynamicColorBuilder. Its builder receives two nullable values:
lightDynamic, the platform's lightColorScheme, when available.darkDynamic, the platform's darkColorScheme, when available.
Either value can be null. This happens on unsupported platforms, older operating systems, or when the platform cannot provide a palette.
Build dynamic and fallback schemes
Use a fixed seed color as a fallback. This gives your app a complete design even when dynamic colors are unavailable.
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const DynamicColorApp());
}
class DynamicColorApp extends StatelessWidget {
const DynamicColorApp({super.key});
static const fallbackSeed = Color(0xFF6750A4);
@override
Widget build(BuildContext context) {
return DynamicColorBuilder(
builder: (lightDynamic, darkDynamic) {
final lightScheme = lightDynamic ??
ColorScheme.fromSeed(
seedColor: fallbackSeed,
);
final darkScheme = darkDynamic ??
ColorScheme.fromSeed(
seedColor: fallbackSeed,
brightness: Brightness.dark,
);
return MaterialApp(
title: 'Dynamic color',
theme: ThemeData(
colorScheme: lightScheme,
useMaterial3: true,
),
darkTheme: ThemeData(
colorScheme: darkScheme,
useMaterial3: true,
),
themeMode: ThemeMode.system,
home: const HomePage(),
);
},
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(title: const Text('Dynamic color')),
body: Center(
child: Container(
padding: const EdgeInsets.all(24),
color: colors.primaryContainer,
child: Text(
'Colors come from the platform when supported.',
style: TextStyle(color: colors.onPrimaryContainer),
),
),
),
);
}
}DynamicColorBuilder may rebuild its contents after platform colors become available. Keep the MaterialApp inside the builder so the whole application receives the correct schemes.
Choose a fallback deliberately
A fallback seed should match your brand and remain readable when converted into light and dark schemes.
| Situation | Recommended behavior |
|---|---|
| Platform supplies both schemes | Use the platform schemes |
| Platform supplies neither scheme | Generate both schemes from your seed |
| Only one scheme is supplied | Use it and generate the missing scheme |
| App needs strict brand colors | Use fixed schemes instead of dynamic colors |
Dynamic color is useful when the app should feel integrated with the device. It is less suitable when brand consistency, screenshots, or regulated visual requirements matter more than personalization.
Test accessibility and platform differences
Test at least these combinations:
- A supported Android version with different wallpapers.
- An unsupported platform using the fallback seed.
- System light mode and system dark mode.
- Text, icons, buttons, and containers using their paired
on...roles.
A color scheme can look acceptable while still failing contrast for small text or disabled controls. Use a contrast checker and verify important states such as errors, selected controls, focus, and disabled content.
Exercise
Why should an app use lightDynamic ?? fallbackLight instead of assuming that lightDynamic is always present?
Show answer
The platform may not support dynamic color or may fail to provide a palette, so the fallback keeps the app usable on every supported platform.