Bird
Raised Fist0
Fluttermobile~20 mins

ThemeData configuration in Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Themed Home Screen
A simple home screen that uses ThemeData to style the app's colors and text styles.
Target UI
-------------------------
| Themed Home Screen     |
|-----------------------|
|                       |
|   Welcome to the app!  |
|                       |
|   Press the button     |
|                       |
|   [Click Me]           |
|                       |
-------------------------
Use ThemeData to set primary color to blue and accent color to orange.
Set the default font family to 'Roboto'.
Style the AppBar with the primary color.
Style the button with the accent color.
Use the theme's text styles for the welcome text.
Starter Code
Flutter
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: 'ThemeData Demo',
      // TODO: Add ThemeData configuration here
      home: const HomeScreen(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Themed Home Screen'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // TODO: Add welcome text styled with theme
            // TODO: Add button styled with theme
          ],
        ),
      ),
    );
  }
}
Task 1
Task 2
Task 3
Task 4
Solution
Flutter
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: 'ThemeData Demo',
      theme: ThemeData(
        primaryColor: Colors.blue,
        colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.orange),
        fontFamily: 'Roboto',
      ),
      home: const HomeScreen(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text('Themed Home Screen'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              'Welcome to the app!',
              style: theme.textTheme.headline6,
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: theme.colorScheme.secondary,
              ),
              onPressed: () {},
              child: const Text('Click Me'),
            ),
          ],
        ),
      ),
    );
  }
}

We added a ThemeData to the MaterialApp to set the primary color to blue and the secondary (accent) color to orange using colorScheme. We also set the default font family to 'Roboto'.

In the HomeScreen, the AppBar automatically uses the primary color from the theme. The welcome text uses the theme's headline6 text style for consistent styling. The button uses ElevatedButton.styleFrom to apply the accent color from the theme's color scheme.

This setup ensures consistent colors and fonts across the app, making it easy to maintain and update the look.

Final Result
Completed Screen
-------------------------
| Themed Home Screen     |
|-----------------------|
|                       |
|   Welcome to the app!  |
|                       |
|   Press the button     |
|                       |
|   [Click Me]           |
|                       |
-------------------------
Tapping the 'Click Me' button triggers an empty onPressed callback (no action).
AppBar is styled with blue background.
Button background is orange.
Text uses Roboto font and theme's headline6 style.
Stretch Goal
Add dark mode support that switches colors automatically based on system theme.
💡 Hint
Use ThemeData.dark() and ThemeData.light() with MaterialApp's themeMode property.

Practice

(1/5)
1. What is the main purpose of ThemeData in a Flutter app?
easy
A. To store app data locally
B. To handle user input events
C. To manage app navigation routes
D. To set colors and styles for the entire app

Solution

  1. Step 1: Understand ThemeData role

    ThemeData is used to define colors, fonts, and styles globally for the app.
  2. Step 2: Compare with other options

    Handling input, navigation, or data storage are unrelated to ThemeData.
  3. Final Answer:

    To set colors and styles for the entire app -> Option D
  4. Quick Check:

    ThemeData = App-wide styles [OK]
Hint: ThemeData controls app look and feel globally [OK]
Common Mistakes:
  • Confusing ThemeData with navigation or data handling
  • Thinking ThemeData manages user input
  • Assuming ThemeData stores app data
2. Which of the following is the correct way to set the primary color in ThemeData?
easy
A. ThemeData(primaryColor: Colors.blue)
B. ThemeData(colorPrimary: Colors.blue)
C. ThemeData(primary: Colors.blue)
D. ThemeData(color: Colors.blue)

Solution

  1. Step 1: Recall ThemeData property names

    The correct property to set the main app color is primaryColor.
  2. Step 2: Check each option

    Only primaryColor is valid; others are incorrect property names.
  3. Final Answer:

    ThemeData(primaryColor: Colors.blue) -> Option A
  4. Quick Check:

    primaryColor sets main color [OK]
Hint: Use primaryColor, not colorPrimary or primary [OK]
Common Mistakes:
  • Using wrong property names like colorPrimary
  • Omitting 'Color' in property name
  • Confusing property with general color
3. What will be the background color of the app if you use this ThemeData?
ThemeData(
  brightness: Brightness.dark,
  scaffoldBackgroundColor: Colors.black,
)
medium
A. Black background
B. White background
C. Blue background
D. Transparent background

Solution

  1. Step 1: Analyze scaffoldBackgroundColor

    The scaffoldBackgroundColor is explicitly set to Colors.black, so the background will be black.
  2. Step 2: Consider brightness effect

    Brightness.dark sets dark theme defaults but does not override explicit scaffoldBackgroundColor.
  3. Final Answer:

    Black background -> Option A
  4. Quick Check:

    scaffoldBackgroundColor = Black [OK]
Hint: scaffoldBackgroundColor sets main screen color [OK]
Common Mistakes:
  • Assuming brightness.dark changes background automatically
  • Ignoring scaffoldBackgroundColor property
  • Confusing background with primaryColor
4. Identify the error in this ThemeData configuration:
ThemeData(
  primaryColor: Colors.red,
  primaryColor: Colors.blue,
)
medium
A. The last primaryColor overrides the first, no error
B. Duplicate primaryColor properties cause a syntax error
C. Colors.red is invalid, should be Colors.redAccent
D. ThemeData cannot have primaryColor property

Solution

  1. Step 1: Check for duplicate keys in Dart constructors

    Dart does not allow duplicate named parameters in constructor calls; it causes a compile-time syntax error.
  2. Step 2: Validate color names and properties

    Colors.red and Colors.blue are valid colors; primaryColor is a valid property.
  3. Final Answer:

    Duplicate primaryColor properties cause a syntax error -> Option B
  4. Quick Check:

    Duplicate named args = syntax error [OK]
Hint: Duplicate named parameters cause syntax error in Dart [OK]
Common Mistakes:
  • Thinking duplicate keys allow last to override
  • Confusing color names with invalid ones
  • Believing primaryColor is invalid property
5. You want your app to use a light theme with green as the primary color and orange for accent color. Which ThemeData configuration achieves this correctly?
hard
A. ThemeData(brightness: Brightness.light, primarySwatch: Colors.green, colorAccent: Colors.orange)
B. ThemeData(brightness: Brightness.dark, primaryColor: Colors.green, secondaryColor: Colors.orange)
C. ThemeData(brightness: Brightness.light, primaryColor: Colors.green, accentColor: Colors.orange)
D. ThemeData(brightness: Brightness.light, primaryColor: Colors.green, colorAccent: Colors.orange)

Solution

  1. Step 1: Set brightness to light

    Use brightness: Brightness.light for a light theme.
  2. Step 2: Use correct properties for colors

    primaryColor sets main color, accentColor sets highlight color. Both must be valid.
  3. Step 3: Check options for property correctness

    ThemeData(brightness: Brightness.light, primaryColor: Colors.green, accentColor: Colors.orange) uses correct properties and values. Others use wrong property names or brightness.
  4. Final Answer:

    ThemeData(brightness: Brightness.light, primaryColor: Colors.green, accentColor: Colors.orange) -> Option C
  5. Quick Check:

    Light theme + primaryColor + accentColor = ThemeData(brightness: Brightness.light, primaryColor: Colors.green, accentColor: Colors.orange) [OK]
Hint: Use brightness.light + primaryColor + accentColor correctly [OK]
Common Mistakes:
  • Using wrong property names like colorAccent or secondaryColor
  • Setting brightness to dark instead of light
  • Confusing primarySwatch with primaryColor
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