Bird
Raised Fist0
Fluttermobile~20 mins

Color schemes in Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Color Scheme Demo
This screen shows how to use light and dark color schemes in a Flutter app. It has a title, a colored box, and a button to switch between light and dark modes.
Target UI
-------------------------
| Color Scheme Demo      |
|-----------------------|
|                       |
|   [Colored Box]        |
|                       |
|   [Switch Theme Button]|
|                       |
-------------------------
Use ThemeData to define light and dark color schemes
Show a colored box that uses the primary color from the current theme
Add a button labeled 'Switch Theme' that toggles between light and dark modes
Update the app bar title color to match the theme's onPrimary color
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 _isDark = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light(),
      darkTheme: ThemeData.dark(),
      themeMode: _isDark ? ThemeMode.dark : ThemeMode.light,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Color Scheme Demo'),
          // TODO: Set title color to onPrimary from theme
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // TODO: Add a colored box using theme primary color
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    _isDark = !_isDark;
                  });
                },
                child: const Text('Switch Theme'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
Task 1
Task 2
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 _isDark = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        colorScheme: ColorScheme.light(
          primary: Colors.blue,
          onPrimary: Colors.white,
        ),
      ),
      darkTheme: ThemeData(
        colorScheme: ColorScheme.dark(
          primary: Colors.deepPurple,
          onPrimary: Colors.white,
        ),
      ),
      themeMode: _isDark ? ThemeMode.dark : ThemeMode.light,
      home: Scaffold(
        appBar: AppBar(
          title: Text(
            'Color Scheme Demo',
            style: TextStyle(
              color: Theme.of(context).colorScheme.onPrimary,
            ),
          ),
          backgroundColor: Theme.of(context).colorScheme.primary,
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                width: 150,
                height: 150,
                color: Theme.of(context).colorScheme.primary,
              ),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    _isDark = !_isDark;
                  });
                },
                child: const Text('Switch Theme'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

This app uses ThemeData with ColorScheme.light and ColorScheme.dark to define colors for light and dark modes. The themeMode switches between them based on a boolean state.

The AppBar background uses the theme's primary color, and the title text color uses colorScheme.onPrimary to ensure good contrast.

The colored box in the center uses the primary color from the current theme, so it changes color when switching themes.

The button toggles the theme mode by updating the state, causing the UI to rebuild with the new colors.

Final Result
Completed Screen
-------------------------
| Color Scheme Demo      |
|-----------------------|
|                       |
|   [ Blue or Purple ]   |
|   [ Colored Box ]      |
|                       |
|   [Switch Theme Button]|
|                       |
-------------------------
Tapping 'Switch Theme' toggles between light (blue) and dark (purple) color schemes.
The colored box changes color to match the theme's primary color.
The app bar background and title text colors update accordingly.
Stretch Goal
Add smooth animation when switching between light and dark themes.
💡 Hint
Use AnimatedTheme widget or wrap MaterialApp with AnimatedBuilder to animate theme changes.

Practice

(1/5)
1. What is the main purpose of using a ColorScheme in a Flutter app?
easy
A. To create animations for color changes
B. To group related colors for consistent app styling
C. To load images with color filters
D. To define font styles for text widgets

Solution

  1. Step 1: Understand ColorScheme role

    A ColorScheme groups colors like primary, secondary, background for consistent use.
  2. Step 2: Identify purpose in app design

    This grouping helps keep colors uniform across the app's widgets and themes.
  3. Final Answer:

    To group related colors for consistent app styling -> Option B
  4. Quick Check:

    ColorScheme = Group colors [OK]
Hint: ColorScheme groups colors for consistent app look [OK]
Common Mistakes:
  • Confusing ColorScheme with animations
  • Thinking it controls fonts or images
  • Using ColorScheme only for backgrounds
2. Which of the following is the correct way to create a light color scheme in Flutter?
easy
A. ThemeData.light(primaryColor: Colors.blue)
B. ColorScheme.dark(primary: Colors.blue)
C. ColorScheme.light(primary: Colors.blue)
D. ThemeData.dark(primaryColor: Colors.blue)

Solution

  1. Step 1: Identify light scheme constructor

    Flutter provides ColorScheme.light() to create a light color scheme.
  2. Step 2: Check usage with primary color

    Passing primary: Colors.blue sets the main color in the scheme.
  3. Final Answer:

    ColorScheme.light(primary: Colors.blue) -> Option C
  4. Quick Check:

    Light scheme uses ColorScheme.light() [OK]
Hint: Use ColorScheme.light() for light mode colors [OK]
Common Mistakes:
  • Using ColorScheme.dark() for light mode
  • Confusing ThemeData with ColorScheme
  • Passing colors to wrong constructors
3. Given this code snippet, what color will the FloatingActionButton use for its background?
final scheme = ColorScheme.dark(primary: Colors.red);
return FloatingActionButton(
  backgroundColor: scheme.primary,
  onPressed: () {},
  child: Icon(Icons.add),
);
medium
A. Blue
B. Green
C. Default grey
D. Red

Solution

  1. Step 1: Analyze ColorScheme.dark usage

    The scheme is dark with primary: Colors.red, so primary color is red.
  2. Step 2: Check FloatingActionButton background

    The button's backgroundColor is set to scheme.primary, which is red.
  3. Final Answer:

    Red -> Option D
  4. Quick Check:

    scheme.primary = red [OK]
Hint: backgroundColor uses scheme.primary color [OK]
Common Mistakes:
  • Assuming default color instead of scheme.primary
  • Confusing dark scheme colors with light scheme
  • Ignoring the assigned primary color
4. What is wrong with this code snippet that tries to create a dark color scheme?
final scheme = ColorScheme.dark(primaryColor: Colors.green);
medium
A. The property should be named 'primary' not 'primaryColor'
B. ColorScheme.dark() does not accept any parameters
C. Colors.green is not allowed in dark schemes
D. Missing import for Colors

Solution

  1. Step 1: Check ColorScheme.dark parameters

    The correct property name is primary, not primaryColor.
  2. Step 2: Understand parameter naming

    Using primaryColor causes a syntax error because it is not recognized.
  3. Final Answer:

    The property should be named 'primary' not 'primaryColor' -> Option A
  4. Quick Check:

    Use 'primary' property in ColorScheme [OK]
Hint: Use 'primary' property, not 'primaryColor' in ColorScheme [OK]
Common Mistakes:
  • Using 'primaryColor' instead of 'primary'
  • Thinking ColorScheme.dark() takes no parameters
  • Assuming Colors.green is invalid in dark mode
5. You want to create a custom color scheme that changes the background color based on light or dark mode. Which approach correctly applies this in Flutter?
hard
A. Use ThemeData.from(colorScheme: ColorScheme.light()) for light mode and ThemeData.from(colorScheme: ColorScheme.dark()) for dark mode, switching based on brightness.
B. Manually set background colors in every widget without using ColorScheme.
C. Use only ColorScheme.light() and ignore dark mode.
D. Set background color directly in Scaffold without using themes.

Solution

  1. Step 1: Understand theme switching

    Flutter apps switch themes by detecting brightness (light or dark mode).
  2. Step 2: Use ThemeData.from with ColorScheme

    Using ThemeData.from(colorScheme: ...) applies the color scheme properly for each mode.
  3. Step 3: Avoid manual color setting

    Manual color setting is error-prone and inconsistent compared to using themes.
  4. Final Answer:

    Use ThemeData.from(colorScheme: ColorScheme.light()) for light mode and ThemeData.from(colorScheme: ColorScheme.dark()) for dark mode, switching based on brightness. -> Option A
  5. Quick Check:

    ThemeData.from + ColorScheme = correct approach [OK]
Hint: Use ThemeData.from with ColorScheme for mode-based colors [OK]
Common Mistakes:
  • Setting colors manually in widgets
  • Ignoring dark mode color schemes
  • Not using ThemeData.from for color schemes
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