Dark mode helps reduce eye strain in low light and saves battery on devices with OLED screens.
Dark mode support in Flutter
MaterialApp( theme: ThemeData.light(), darkTheme: ThemeData.dark(), themeMode: ThemeMode.system, home: MyHomePage(), )
theme sets the light mode style.
darkTheme sets the dark mode style.
themeMode controls which theme is active: light, dark, or system default.
MaterialApp( theme: ThemeData.light(), darkTheme: ThemeData.dark(), themeMode: ThemeMode.dark, home: MyHomePage(), )
MaterialApp( theme: ThemeData.light(), darkTheme: ThemeData.dark(), themeMode: ThemeMode.light, home: MyHomePage(), )
MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.light,
),
darkTheme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.dark,
),
themeMode: ThemeMode.system,
home: MyHomePage(),
)This app automatically switches between light and dark themes based on the device setting. The text and background colors adjust accordingly.
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Dark Mode Demo', theme: ThemeData( primarySwatch: Colors.blue, brightness: Brightness.light, ), darkTheme: ThemeData( primarySwatch: Colors.blue, brightness: Brightness.dark, ), themeMode: ThemeMode.system, home: const MyHomePage(), ); } } class MyHomePage extends StatelessWidget { const MyHomePage({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Dark Mode Support'), ), body: const Center( child: Text('Switch your device theme to see changes!'), ), ); } }
Use ThemeMode.system to respect the user's device theme preference.
You can customize colors in ThemeData for a unique look in dark mode.
Test your app in both modes to ensure good readability and contrast.
Dark mode reduces eye strain and saves battery on some devices.
Use theme, darkTheme, and themeMode in MaterialApp to support dark mode.
Test your app in both light and dark modes for best user experience.
Practice
darkTheme in a Flutter app?Solution
Step 1: Understand the role of
ThedarkThemedarkThemeproperty inMaterialAppdefines how the app looks when dark mode is active.Step 2: Compare with other theme properties
Thethemeproperty is for light mode, anddarkThemeis specifically for dark mode appearance.Final Answer:
To define the app's appearance when the device is in dark mode -> Option AQuick Check:
darkThemesets dark mode style [OK]
- Confusing darkTheme with theme (light mode)
- Thinking darkTheme disables dark mode
- Assuming darkTheme affects only fonts
MaterialApp?Solution
Step 1: Identify how to activate dark mode
UsingthemeMode: ThemeMode.darktells Flutter to use the dark theme.Step 2: Provide the dark theme style
ProvidingdarkThemedefines the dark mode appearance.Final Answer:
SetthemeMode: ThemeMode.darkand providedarkTheme-> Option AQuick Check:
Enable dark mode with themeMode and darkTheme [OK]
- Only setting theme without themeMode
- Using light theme in darkTheme property
- Setting themeMode to light but expecting dark mode
MaterialApp(
theme: ThemeData(primaryColor: Colors.white),
darkTheme: ThemeData(primaryColor: Colors.black),
themeMode: ThemeMode.system,
home: Scaffold(
backgroundColor: Theme.of(context).primaryColor,
),
)Solution
Step 1: Understand themeMode.system behavior
WhenthemeModeissystem, Flutter usesdarkThemeif device is in dark mode.Step 2: Check primaryColor in darkTheme
ThedarkThemesetsprimaryColorto black, so backgroundColor will be black.Final Answer:
Black -> Option CQuick Check:
System dark mode uses darkTheme colors [OK]
- Assuming theme (light) colors always apply
- Ignoring themeMode.system behavior
- Confusing primaryColor with backgroundColor default
MaterialApp( theme: ThemeData.light(), darkTheme: ThemeData.dark(), home: HomeScreen(), )
Solution
Step 1: Check themeMode usage
WithoutthemeMode, Flutter defaults to light mode and ignores darkTheme.Step 2: Confirm darkTheme is correct
ThemeData.dark()is valid, so no error there.Final Answer:
MissingthemeModeproperty to switch themes -> Option BQuick Check:
themeMode is required to enable dark mode switching [OK]
- Forgetting to set themeMode
- Thinking darkTheme alone enables dark mode
- Wrapping home widget unnecessarily
Solution
Step 1: Understand system and user control
To follow system theme and allow manual override,themeModemust be dynamic based on user choice.Step 2: Implement themeMode with user state
Store user preference in app state and setthemeModeaccordingly, while providingthemeanddarkTheme.Final Answer:
Usetheme,darkTheme, and controlthemeModewith a user setting stored in state -> Option DQuick Check:
Dynamic themeMode enables system and user control [OK]
- Ignoring user preferences with fixed themeMode
- Manually changing colors in widgets instead of themes
- Restarting app to apply theme changes
Ready to prove it?
Pass the test and this pattern moves unknown
