Sub topic 8
System Colors and Dark Mode
Assume you know CupertinoApp, basic widget composition, and how BuildContext gives widgets access to inherited theme data. This chapter focuses on choosing colors that remain readable when the interface changes between light and dark mode.
Use semantic system colors
A semantic color describes a role instead of a fixed hue. For example, CupertinoColors.systemBackground means “the main background for this interface.” Flutter can resolve it to a light or dark value.
Common semantic colors include:
systemBackgroundfor the main page background.secondarySystemBackgroundfor grouped surfaces, cards, or panels.labelfor primary text.secondaryLabelfor supporting text.systemBlue,systemGreen, and other system accent colors.separatorfor lines between rows.
These colors are different from fixed colors such as CupertinoColors.white and CupertinoColors.black. Fixed colors do not change when the brightness changes.
| Use case | Prefer | Avoid |
|---|---|---|
| Page background | `CupertinoColors.systemBackground` | `CupertinoColors.white` |
| Primary text | `CupertinoColors.label` | A hard-coded black |
| Secondary text | `CupertinoColors.secondaryLabel` | A fixed grey |
| Divider | `CupertinoColors.separator` | A fixed light grey |
Resolve colors in custom widgets
Cupertino widgets usually resolve dynamic colors for you. Custom painting and some lower-level APIs may need an ordinary Color. Use CupertinoDynamicColor.resolve with the current context.
import 'package:flutter/cupertino.dart';
void main() {
runApp(const CupertinoApp(home: ColorPage()));
}
class ColorPage extends StatelessWidget {
const ColorPage({super.key});
@override
Widget build(BuildContext context) {
final background = CupertinoDynamicColor.resolve(
CupertinoColors.systemBackground,
context,
);
final label = CupertinoDynamicColor.resolve(
CupertinoColors.label,
context,
);
final secondaryLabel = CupertinoDynamicColor.resolve(
CupertinoColors.secondaryLabel,
context,
);
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text('Appearance'),
),
backgroundColor: CupertinoColors.systemBackground,
child: SafeArea(
child: ColoredBox(
color: background,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'System colors',
style: TextStyle(color: label, fontSize: 22),
),
const SizedBox(height: 8),
Text(
'This text remains readable in both modes.',
style: TextStyle(color: secondaryLabel),
),
],
),
),
),
),
);
}
}When the operating system changes brightness, Flutter rebuilds widgets that depend on the relevant inherited data. The same semantic color then resolves to its light or dark variant.
Control brightness deliberately
A CupertinoApp can follow the device brightness when you leave the theme brightness unspecified. You can also provide a CupertinoThemeData with an explicit brightness when building a preview or testing a particular appearance.
Avoid choosing colors only by checking whether the current mode is dark. Semantic colors encode the intended role and usually provide better contrast than manually selecting a dark or light shade.
Exercise
Why is CupertinoColors.systemBackground safer than CupertinoColors.white for a page background?
Show answer
systemBackground resolves to a suitable light or dark surface, while white remains white and can reduce contrast in dark mode.