Sub topic 8
System colors and dark mode
This chapter assumes you can build a basic CupertinoApp screen with CupertinoPageScaffold, as in the earlier chapters, and that you know BuildContext and MediaQuery at a basic level. It covers how Cupertino colors change between light and dark mode, where that change happens, and the cases where it silently doesn't.
Why Cupertino colors are not plain colors
On iOS, system colors are semantic. A "label" color is near-black in light mode and near-white in dark mode. A "system background" is white in light mode and black in dark mode. Apps ask for the role, and the system supplies the shade.
Flutter models this with CupertinoDynamicColor. It is a subclass of Color that carries several variants of one color:
- A light color and a dark color.
- High-contrast versions of each, used when the user turns on "Increase Contrast".
- Optional "elevated" versions of each, used for surfaces that sit above others, such as a sheet over a page.
CupertinoColors exposes many of these as constants. Because a CupertinoDynamicColor is still a Color, it compiles anywhere a Color is expected. That makes it easy to use one without it ever being resolved.
The semantic colors you will use most
| Constant | Role | Light | Dark |
|---|---|---|---|
| `CupertinoColors.label` | Primary text | Near-black | Near-white |
| `CupertinoColors.secondaryLabel` | Subtitles, captions | Gray | Lighter gray |
| `CupertinoColors.systemBackground` | Page background | White | Black |
| `CupertinoColors.secondarySystemBackground` | Cards on a page | Light gray | Dark gray |
| `CupertinoColors.systemGroupedBackground` | Behind grouped lists | Light gray | Black |
| `CupertinoColors.separator` | Dividers | Translucent gray | Translucent gray |
| `CupertinoColors.systemBlue` | Accent colors | Standard blue | Brighter blue |
Several accent colors have the same shape: systemRed, systemGreen, systemIndigo and so on. Their dark variants are slightly brighter so they stay readable on black.
CupertinoColors.white, CupertinoColors.black and CupertinoColors.activeBlue (in older code) are different. white and black are fixed colors and never change with brightness. Use them only when you truly want a fixed color, such as text on a saturated button.
How resolution works
A dynamic color is not "light" or "dark" until it is resolved against a BuildContext. Resolution reads three things from the context:
- The brightness, from
CupertinoTheme.brightnessOf(context). If the theme'sbrightnessis null, this falls back toMediaQuery.platformBrightnessOf(context), which is the device setting. - Whether high contrast is on, from
MediaQuery.highContrastOf(context). - The interface level (base or elevated), from
CupertinoUserInterfaceLevel.
The key point is that CupertinoThemeData.brightness wins over the device setting. If you set it to Brightness.dark, the app stays dark even when the phone is in light mode. If you leave it null, the app follows the device.
Resolving colors yourself
Cupertino widgets resolve their own default colors. CupertinoPageScaffold, CupertinoNavigationBar and the text in a CupertinoTheme already adapt. You have to resolve only the colors you supply yourself, such as a Container background or a TextStyle color.
There are two equivalent ways to resolve a color:
color.resolveFrom(context)on aCupertinoDynamicColor.CupertinoDynamicColor.resolve(color, context), a static method that accepts anyColorand returns it unchanged if it is not dynamic.
import 'package:flutter/cupertino.dart';
class ProfileCard extends StatelessWidget {
const ProfileCard({super.key});
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: CupertinoColors.secondarySystemGroupedBackground
.resolveFrom(context),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: CupertinoColors.separator.resolveFrom(context),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Ada Lovelace',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w600,
color: CupertinoColors.label.resolveFrom(context),
),
),
Text(
'Mathematician',
style: TextStyle(
color: CupertinoColors.secondaryLabel.resolveFrom(context),
),
),
],
),
);
}
}Call resolveFrom inside build, not in initState or a field initializer. The result depends on the context, and it must be recomputed when brightness changes. A widget that rebuilds because the platform brightness changed will pick up the new value only if resolveFrom runs again during that build.
Defining your own dynamic colors
For brand colors that need a dark variant, build a CupertinoDynamicColor instead of branching on brightness in every widget:
import 'package:flutter/cupertino.dart';
const brandSurface = CupertinoDynamicColor.withBrightness(
color: Color(0xFFEFF3FF),
darkColor: Color(0xFF1B2440),
);
const brandAccent = CupertinoDynamicColor.withBrightnessAndContrast(
color: Color(0xFF3050D0),
darkColor: Color(0xFF8CA2FF),
highContrastColor: Color(0xFF1530A0),
darkHighContrastColor: Color(0xFFB8C6FF),
);Use withBrightness when you only care about light and dark. Use withBrightnessAndContrast when you also want to honor the high-contrast setting. Both constructors are const, so these can be top-level constants. You resolve them the same way as system colors: brandSurface.resolveFrom(context).
Putting dynamic colors in the theme
CupertinoThemeData resolves its own color properties, so you can pass dynamic colors there without calling resolveFrom:
import 'package:flutter/cupertino.dart';
class ThemedApp extends StatelessWidget {
const ThemedApp({super.key});
@override
Widget build(BuildContext context) {
return const CupertinoApp(
theme: CupertinoThemeData(
primaryColor: brandAccent,
scaffoldBackgroundColor: CupertinoColors.systemGroupedBackground,
),
home: CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(middle: Text('Home')),
child: SafeArea(child: Center(child: Text('Hello'))),
),
);
}
}
const brandAccent = CupertinoDynamicColor.withBrightness(
color: Color(0xFF3050D0),
darkColor: Color(0xFF8CA2FF),
);When you read values back, use the theme getters on CupertinoTheme.of(context). For example, CupertinoTheme.of(context).primaryColor returns a resolved color.
To force one mode regardless of the device, set brightness:
const CupertinoThemeData(brightness: Brightness.dark)Leave brightness null if you want the app to follow the system. Forcing a mode is reasonable for something like a media player, but users generally expect an app to respect their device setting.
Dark mode inside a MaterialApp
In a MaterialApp, the Material Theme supplies a Cupertino theme derived from ThemeData. Its brightness comes from ThemeData.brightness, which follows themeMode and the device setting. Cupertino widgets and resolveFrom calls inside the app therefore resolve against the Material brightness. To switch the whole app, change themeMode or the brightness in ThemeData. No separate Cupertino setup is needed.
Testing both modes
Force the brightness in the theme and assert on the resolved color. Checking luminance avoids comparing CupertinoDynamicColor objects with Color objects, whose equality rules differ.
import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';
Future<Color> _resolvedBackground(
WidgetTester tester,
Brightness brightness,
) async {
late Color resolved;
await tester.pumpWidget(
CupertinoApp(
theme: CupertinoThemeData(brightness: brightness),
home: Builder(
builder: (context) {
resolved = CupertinoColors.systemBackground.resolveFrom(context);
return const SizedBox();
},
),
),
);
return resolved;
}
void main() {
testWidgets('systemBackground is light in light mode', (tester) async {
final color = await _resolvedBackground(tester, Brightness.light);
expect(color.computeLuminance(), greaterThan(0.9));
});
testWidgets('systemBackground is dark in dark mode', (tester) async {
final color = await _resolvedBackground(tester, Brightness.dark);
expect(color.computeLuminance(), lessThan(0.1));
});
}To test the "follow the device" path, leave the theme brightness null and set tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark. Register addTearDown(tester.platformDispatcher.clearAllTestValues) so the value does not leak into other tests.
Common mistakes
- Using
CupertinoColors.whiteorblackfor surfaces. They never change. UsesystemBackgroundandlabelfor roles that should adapt. - Resolving once and caching. A cached color is stale after the user changes brightness. Resolve in
build. - Hard-coded hex colors for text and backgrounds. They look fine in one mode and unreadable in the other. Wrap them in a
CupertinoDynamicColor. - Forcing
brightnessand forgetting it. A hard-codedBrightness.lightin the theme ignores dark mode on every device. - Assuming translucent colors are opaque.
separatorhas alpha. Drawn over a colored background, it blends with that background.
Exercise
A Container uses color: CupertinoColors.systemBackground. The app is a CupertinoApp with no brightness set in CupertinoThemeData. On a phone in dark mode, the Container is white. What is wrong, and what is the fix?
Show answer
The dynamic color was never resolved, so it paints as its default light variant. Resolve it against the context inside build: color: CupertinoColors.systemBackground.resolveFrom(context). Because the theme brightness is null, resolution then follows the device's dark setting and the Container turns black.
Exercise
You need a card color that is #F2F2F7 in light mode and #1C1C1E in dark mode, and you want it usable as a const. Write the declaration, and say how you read the correct variant in a widget.
Show answer
const cardColor = CupertinoDynamicColor.withBrightness(color: Color(0xFFF2F2F7), darkColor: Color(0xFF1C1C1E)); In a widget's build method, call cardColor.resolveFrom(context) and use the result as the Container or BoxDecoration color.