DEV Community

Cover image for Flutter Dark Mode: ThemeMode Done Right
Muhammad Uzair
Muhammad Uzair

Posted on Originally published at uzairxdev.online

Flutter Dark Mode: ThemeMode Done Right

Most users expect every app to offer dark mode, and many set their whole phone to dark and never look back. An app that stays blindingly white at night feels broken, and an app that forces a dark theme on someone who prefers light feels just as wrong. Flutter makes dark mode easy to add, but easy to get wrong: a single hardcoded white container or a forgotten status bar color is enough to break the experience.

This guide shows how to implement dark mode in Flutter the right way, using ThemeMode and separate light and dark ThemeData objects, a user-facing switch that remembers its choice, and colors that stay readable in both modes. Everything here works with the current stable Flutter release and the Material 3 defaults.

What is ThemeMode in Flutter?

ThemeMode is the enum that tells a MaterialApp which theme to show. It has three values: ThemeMode.system follows the device setting, ThemeMode.light always shows the light theme, and ThemeMode.dark always shows the dark theme. You hand MaterialApp a theme for light mode and a darkTheme for dark mode, and Flutter swaps between them automatically when the mode changes, with no rebuild of your widget tree and no manual color checks in every screen.

The default is ThemeMode.system, which means your app already respects the device setting as soon as you supply a darkTheme. That is the behaviour most users expect: the app follows the phone. A user-facing setting should therefore offer three choices, System, Light, and Dark, instead of a single on and off toggle, because a toggle cannot express "follow the system" and silently takes that choice away.

\dart
MaterialApp(
title: 'My App',
theme: lightTheme,
darkTheme: darkTheme,
themeMode: ThemeMode.system,
home: const HomeScreen(),
)
\
\

When themeMode is ThemeMode.system, Flutter reads the platform brightness from the operating system and picks the matching theme. Changing the phone setting while the app is open switches the theme live, because MaterialApp listens for platform brightness changes and rebuilds with the new theme. For more on building layouts that adapt to the device, see Flutter Responsive Design for Every Screen.

How do you define light and dark themes in Flutter?

Define light and dark themes as two ThemeData objects built from the same design tokens, so the two modes differ only in color. Start from ColorScheme.fromSeed with a seed color and an explicit brightness for each mode, then let the color scheme drive the colors of buttons, cards, and text instead of setting colors widget by widget. That keeps the two themes consistent, and a change to the seed color updates both modes at once.

Keep the structure of both themes identical: the same text styles, the same shapes, the same spacing, only different colors. If the dark theme changes a font size or a corner radius, the layout shifts every time the theme changes, which looks like a bug. Define the shared parts once and build both ThemeData objects from them.

\`dart
final lightTheme = ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.light,
),
useMaterial3: true,
);

final darkTheme = ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.dark,
),
useMaterial3: true,
);
`\

ColorScheme.fromSeed generates a full set of matching colors from one seed color, and passing brightness tells it whether to generate the light or the dark set. With Material 3, the generated dark scheme uses dark surfaces and light text automatically, so a seeded scheme is the fastest way to a dark theme that looks designed rather than inverted.

How do you let users switch themes in Flutter?

A theme switch needs three pieces: a place to store the current ThemeMode, a widget that rebuilds MaterialApp when it changes, and a control the user can tap. A small ChangeNotifier holding a ThemeMode, exposed with Provider, covers the first two, and a segmented button or dropdown in Settings covers the third. Keep the controller above MaterialApp in the widget tree so changing the mode rebuilds the app with the new theme.

Persist the choice with SharedPreferences so the app opens in the theme the user picked last time. Read the saved value before runApp, pass it into the controller as the initial mode, and save on every change. If nothing is saved yet, start with ThemeMode.system so new users get the device setting.

\`dart
class ThemeController extends ChangeNotifier {
ThemeController(this._mode);

ThemeMode _mode;

ThemeMode get mode => _mode;

Future setMode(ThemeMode mode) async {
_mode = mode;
notifyListeners();
final prefs = await SharedPreferences.getInstance();
await prefs.setString('theme_mode', mode.name);
}
}

Future main() async {
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
final saved = prefs.getString('theme_mode');
final initial = ThemeMode.values.firstWhere(
(mode) => mode.name == saved,
orElse: () => ThemeMode.system,
);
runApp(
ChangeNotifierProvider(
create: (_) => ThemeController(initial),
child: const MyApp(),
),
);
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
final mode = context.watch().mode;
return MaterialApp(
theme: lightTheme,
darkTheme: darkTheme,
themeMode: mode,
home: const HomeScreen(),
);
}
}
`\

Reading the saved value before runApp matters: if the app starts in the wrong theme and switches a moment later, the user sees a white flash on launch in a dark room. Loading the preference first means the first frame is already the right theme.

How do you read the current brightness inside a widget?

Use Theme.of(context).brightness inside a widget to find out which theme is active. It returns Brightness.dark when the dark theme is showing and Brightness.light otherwise, and it follows the user's choice rather than the device setting, so it stays correct when the user overrides the system mode. Use it for the few places that genuinely need a brightness-aware decision, such as choosing between two versions of an image asset or a chart color.

Do not reach for MediaQuery.platformBrightnessOf(context) for the same question, because it reports the device setting and ignores the user's in-app choice. When the user picks Light while the phone is in dark mode, MediaQuery still reports dark, and any widget that trusts it renders the wrong colors. Theme.of(context).brightness is the single source of truth for what the user actually sees.

\dart
final isDark = Theme.of(context).brightness == Brightness.dark;
final logo = isDark ? 'assets/logo_dark.png' : 'assets/logo_light.png';
\
\

What colors work in a dark theme?

A good dark theme is dark grey, not black. Material Design recommends a dark grey surface, such as #121212, because pure black makes shadows invisible, flattens depth, and creates harsh contrast with white text that strains the eyes in low light. Elevation is shown with lighter overlays on the surface instead of shadows: a card sitting higher above the background is a slightly lighter grey than the background behind it, which is how the dark scheme generated by ColorScheme.fromSeed behaves.

Text in dark mode should be white at reduced opacity, not pure white. Headings and primary text work well at roughly 87 percent white, secondary text at 60 percent, and disabled text at 38 percent. Pure white on near-black has the highest possible contrast, which sounds good but tires the eyes quickly. The default dark text theme in Flutter already applies these opacities, so custom text styles should follow the same rule.

Avoid saturated colors in dark mode. A bright red or vivid blue that looks fine on a white background vibrates against a dark one and dominates the screen. Dark themes need the same hues at lower saturation, which is exactly what a generated dark color scheme does with the seed color. If a brand color must appear, use it sparingly, as an accent, a selected state, or a single button, and never as a large background. The same restraint applies to the rest of the interface: dark mode is an accessibility feature as much as a style choice, and the same care that goes into Color-Blind Friendly Games in Flutter pays off here too.

What breaks when you add dark mode?

The most common dark mode bug is a hardcoded color: a Container with color set to white, a Text widget with a fixed dark color, or a Card that sets its own background. These widgets look right in light mode and wrong in dark mode, because they ignore the theme entirely. Search for hardcoded white, black, and literal hex colors in your widgets and replace them with theme colors such as Theme.of(context).colorScheme.surface or colorScheme.onSurface, which resolve to the right value in both modes.

Images are the next trap: a logo drawn for a white background disappears on a dark one, and a photo with a white frame looks like a mistake. Provide a dark variant of any image that contains text or a logo, and choose between them with Theme.of(context).brightness. Photos themselves need no change.

The status bar and navigation bar need attention too. Flutter sets the status bar icon brightness from the AppBar theme by default, but a full-screen page without an AppBar can leave dark icons on a dark background. Set SystemUiOverlayStyle explicitly on screens that hide the AppBar, or wrap the page in an AnnotatedRegion with a style that matches the theme.

Problem What you see in dark mode Fix
Hardcoded white container White box floating on a dark background Use colorScheme.surface
Fixed dark text color Text invisible on a dark surface Use colorScheme.onSurface
Logo drawn for white Logo vanishes on dark Provide a dark variant asset
Status bar style missing Dark icons on a dark background Set SystemUiOverlayStyle per theme

How do you test dark mode in Flutter?

Test dark mode in a widget test by pumping the widget inside a MaterialApp with themeMode set to ThemeMode.dark, then asserting on the brightness the widget reads. A golden test goes further: render the same screen once in light mode and once in dark mode and compare both images, which catches hardcoded colors and invisible text that a unit assertion would miss.

Test on a real device too, with the system in dark mode and the app set to System, Light, and Dark in turn. Screenshots at both settings are the fastest way to review every screen, because a theme bug is always visual: a white flash on launch, a toolbar that stays light, or a dialog that keeps its light background.

\`dart
testWidgets('home screen renders in dark mode', (tester) async {
await tester.pumpWidget(
MaterialApp(
theme: lightTheme,
darkTheme: darkTheme,
themeMode: ThemeMode.dark,
home: const HomeScreen(),
),
);
await tester.pumpAndSettle();

final context = tester.element(find.byType(HomeScreen));
expect(Theme.of(context).brightness, Brightness.dark);
});
`\

Dark mode checklist before you ship

Run through this list before the release, in both themes:

  • Provide both theme and darkTheme on MaterialApp, and keep ThemeMode.system as the default.
  • Offer System, Light, and Dark as three choices, not a single toggle.
  • Persist the choice and restore it before the first frame, so the app never flashes the wrong theme on launch.
  • Build both themes from one ColorScheme seed so they stay consistent.
  • Replace every hardcoded color with a theme color, and provide dark variants of logo and text images.
  • Check the status bar, dialogs, bottom sheets, and snack bars in both modes, since these are the surfaces developers forget.
  • Test both themes with golden tests and on a real device.

Dark mode done right is a small amount of structure: two ThemeData objects from one seed, a ThemeMode the user controls, persistence across launches, and no hardcoded colors anywhere. Get that structure in place and every new screen inherits dark mode for free, which is the real payoff. Theming becomes a property of the app, not a chore repeated on every page.

Get in touch and let's talk about your project.


Originally published at uzairxdev.online

Top comments (0)