Bird
Raised Fist0
Fluttermobile~20 mins

Dark mode support in Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Dark Mode Demo
This screen shows a simple app that supports switching between light and dark modes using a toggle switch.
Target UI
┌─────────────────────────────┐
│ Dark Mode Demo              │
├─────────────────────────────┤
│                             │
│  Light Mode                 │
│                             │
│  [Toggle Dark Mode Switch]  │
│                             │
└─────────────────────────────┘
Use MaterialApp with theme and darkTheme properties
Add a toggle switch to switch between light and dark mode
Switching the toggle updates the app theme immediately
Show text indicating current mode: 'Light Mode' or 'Dark Mode'
Use StatefulWidget to manage theme state
Starter Code
Flutter
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isDarkMode = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dark Mode Demo',
      theme: ThemeData.light(),
      darkTheme: ThemeData.dark(),
      themeMode: _isDarkMode ? ThemeMode.dark : ThemeMode.light,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Dark Mode Demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                _isDarkMode ? 'Dark Mode' : 'Light Mode',
                style: const TextStyle(fontSize: 24),
              ),
              const SizedBox(height: 20),
              // TODO: Add a Switch widget here to toggle dark mode
            ],
          ),
        ),
      ),
    );
  }
}
Task 1
Task 2
Task 3
Solution
Flutter
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isDarkMode = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dark Mode Demo',
      theme: ThemeData.light(),
      darkTheme: ThemeData.dark(),
      themeMode: _isDarkMode ? ThemeMode.dark : ThemeMode.light,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Dark Mode Demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                _isDarkMode ? 'Dark Mode' : 'Light Mode',
                style: const TextStyle(fontSize: 24),
              ),
              const SizedBox(height: 20),
              Switch(
                value: _isDarkMode,
                onChanged: (bool value) {
                  setState(() {
                    _isDarkMode = value;
                  });
                },
                activeColor: Theme.of(context).colorScheme.secondary,
                inactiveThumbColor: Colors.grey,
                inactiveTrackColor: Colors.grey.shade400,
                semanticLabel: 'Toggle dark mode',
              ),
            ],
          ),
        ),
      ),
    );
  }
}

This app uses a StatefulWidget to keep track of whether dark mode is on or off with the _isDarkMode boolean.

The MaterialApp has theme for light mode and darkTheme for dark mode. The themeMode property switches between them based on _isDarkMode.

Inside the screen, a Switch widget lets the user toggle dark mode. When toggled, it calls setState to update _isDarkMode, which rebuilds the app with the new theme.

The text above the switch updates to show "Light Mode" or "Dark Mode" so the user knows the current mode.

This approach follows Flutter's recommended way to support dark mode with easy switching.

Final Result
Completed Screen
┌─────────────────────────────┐
│ Dark Mode Demo              │
├─────────────────────────────┤
│                             │
│        Dark Mode            │
│                             │
│        [  Switch  ]         │
│                             │
└─────────────────────────────┘
User taps the switch to toggle dark mode on or off.
The background and text colors change immediately to match the selected mode.
The text label updates to show 'Dark Mode' or 'Light Mode' accordingly.
Stretch Goal
Add a system theme sync so the app automatically uses the device's dark or light mode by default.
💡 Hint
Use ThemeMode.system for themeMode property and remove manual toggle or keep toggle to override system setting.

Practice

(1/5)
1. What is the main purpose of using darkTheme in a Flutter app?
easy
A. To define the app's appearance when the device is in dark mode
B. To disable dark mode support in the app
C. To set the default font style for the app
D. To create a theme for tablet devices only

Solution

  1. Step 1: Understand the role of darkTheme

    The darkTheme property in MaterialApp defines how the app looks when dark mode is active.
  2. Step 2: Compare with other theme properties

    The theme property is for light mode, and darkTheme is specifically for dark mode appearance.
  3. Final Answer:

    To define the app's appearance when the device is in dark mode -> Option A
  4. Quick Check:

    darkTheme sets dark mode style [OK]
Hint: Remember: darkTheme is for dark mode look [OK]
Common Mistakes:
  • Confusing darkTheme with theme (light mode)
  • Thinking darkTheme disables dark mode
  • Assuming darkTheme affects only fonts
2. Which of the following is the correct way to enable dark mode support in MaterialApp?
easy
A. Set themeMode: ThemeMode.dark and provide darkTheme
B. Set theme: ThemeData.dark() only
C. Set darkTheme: ThemeData.light() only
D. Set themeMode: ThemeMode.light and provide darkTheme

Solution

  1. Step 1: Identify how to activate dark mode

    Using themeMode: ThemeMode.dark tells Flutter to use the dark theme.
  2. Step 2: Provide the dark theme style

    Providing darkTheme defines the dark mode appearance.
  3. Final Answer:

    Set themeMode: ThemeMode.dark and provide darkTheme -> Option A
  4. Quick Check:

    Enable dark mode with themeMode and darkTheme [OK]
Hint: Use themeMode with darkTheme to enable dark mode [OK]
Common Mistakes:
  • Only setting theme without themeMode
  • Using light theme in darkTheme property
  • Setting themeMode to light but expecting dark mode
3. Given this Flutter code snippet, what will be the background color when the device is in dark mode?
MaterialApp(
  theme: ThemeData(primaryColor: Colors.white),
  darkTheme: ThemeData(primaryColor: Colors.black),
  themeMode: ThemeMode.system,
  home: Scaffold(
    backgroundColor: Theme.of(context).primaryColor,
  ),
)
medium
A. Blue (default)
B. White
C. Black
D. Transparent

Solution

  1. Step 1: Understand themeMode.system behavior

    When themeMode is system, Flutter uses darkTheme if device is in dark mode.
  2. Step 2: Check primaryColor in darkTheme

    The darkTheme sets primaryColor to black, so backgroundColor will be black.
  3. Final Answer:

    Black -> Option C
  4. Quick Check:

    System dark mode uses darkTheme colors [OK]
Hint: themeMode.system uses darkTheme on dark devices [OK]
Common Mistakes:
  • Assuming theme (light) colors always apply
  • Ignoring themeMode.system behavior
  • Confusing primaryColor with backgroundColor default
4. You wrote this code but dark mode is not working as expected. What is the error?
MaterialApp(
  theme: ThemeData.light(),
  darkTheme: ThemeData.dark(),
  home: HomeScreen(),
)
medium
A. theme and darkTheme cannot be used together
B. Missing themeMode property to switch themes
C. Incorrect use of ThemeData.dark()
D. HomeScreen must be wrapped in a Theme widget

Solution

  1. Step 1: Check themeMode usage

    Without themeMode, Flutter defaults to light mode and ignores darkTheme.
  2. Step 2: Confirm darkTheme is correct

    ThemeData.dark() is valid, so no error there.
  3. Final Answer:

    Missing themeMode property to switch themes -> Option B
  4. Quick Check:

    themeMode is required to enable dark mode switching [OK]
Hint: Always set themeMode to enable dark mode [OK]
Common Mistakes:
  • Forgetting to set themeMode
  • Thinking darkTheme alone enables dark mode
  • Wrapping home widget unnecessarily
5. You want your Flutter app to automatically switch between light and dark themes based on system settings, but also allow users to manually override the theme. Which approach correctly implements this behavior?
hard
A. Use darkTheme only and switch themes by restarting the app
B. Set themeMode: ThemeMode.system only and ignore user preferences
C. Use only theme and change colors manually in widgets
D. Use theme, darkTheme, and control themeMode with a user setting stored in state

Solution

  1. Step 1: Understand system and user control

    To follow system theme and allow manual override, themeMode must be dynamic based on user choice.
  2. Step 2: Implement themeMode with user state

    Store user preference in app state and set themeMode accordingly, while providing theme and darkTheme.
  3. Final Answer:

    Use theme, darkTheme, and control themeMode with a user setting stored in state -> Option D
  4. Quick Check:

    Dynamic themeMode enables system and user control [OK]
Hint: Control themeMode with user state for manual override [OK]
Common Mistakes:
  • Ignoring user preferences with fixed themeMode
  • Manually changing colors in widgets instead of themes
  • Restarting app to apply theme changes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard