Bird
Raised Fist0
Fluttermobile~20 mins

MaterialApp and Scaffold in Flutter - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MaterialApp and Scaffold Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What is the output of this Flutter code snippet?

Consider this Flutter app code:

MaterialApp(
  home: Scaffold(
    appBar: AppBar(title: Text('Hello')),
    body: Center(child: Text('Welcome')),
  ),
)

What will the user see on the screen?

Flutter
MaterialApp(
  home: Scaffold(
    appBar: AppBar(title: Text('Hello')),
    body: Center(child: Text('Welcome')),
  ),
)
AA screen with a top bar showing 'Hello' and centered text 'Welcome' below it.
BA blank screen with no text or app bar visible.
COnly the text 'Welcome' centered on a white background, no app bar.
DAn error because Scaffold cannot be used inside MaterialApp.
Attempts:
2 left
💡 Hint

Think about what Scaffold provides inside a MaterialApp.

❓ lifecycle
intermediate
2:00remaining
What happens if you remove MaterialApp and use Scaffold directly?

Given this Flutter code:

Scaffold(
  appBar: AppBar(title: Text('Hi')),
  body: Center(child: Text('Content')),
)

What is the likely result when running this as the root widget?

AThe app crashes because MaterialApp is required for Scaffold.
BThe app runs normally with app bar and content displayed correctly.
CThe app runs but the app bar and material styles are missing or broken.
DThe app shows a blank white screen with no widgets visible.
Attempts:
2 left
💡 Hint

MaterialApp provides theme and material design context.

❓ navigation
advanced
2:00remaining
How to navigate to a new screen using MaterialApp routes?

Given this MaterialApp setup:

MaterialApp(
  routes: {
    '/': (context) => HomeScreen(),
    '/second': (context) => SecondScreen(),
  },
)

Which code snippet correctly navigates from HomeScreen to SecondScreen?

ANavigator.pushNamed(context, '/second');
BNavigator.pop(context, '/second');
CNavigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen()));
DNavigator.pushReplacementNamed(context, '/home');
Attempts:
2 left
💡 Hint

Use the named route defined in MaterialApp routes.

📝 Syntax
advanced
2:00remaining
What error does this Flutter code produce?

Analyze this Flutter snippet:

MaterialApp(
  home: Scaffold(
    appBar: AppBar(title Text('Title')),
    body: Text('Body'),
  ),
)

What error will occur?

ARuntimeError: Scaffold cannot have Text directly as body.
BNo error, code runs fine showing app bar and body text.
CTypeError: AppBar title expects a Widget but got a String.
DSyntaxError: Missing colon after 'title' in AppBar constructor.
Attempts:
2 left
💡 Hint

Check the AppBar constructor syntax carefully.

🧠 Conceptual
expert
2:00remaining
Why is MaterialApp usually the root widget in Flutter apps?

Choose the best explanation for why MaterialApp is typically the root widget in Flutter apps.

AMaterialApp is required to display any widgets on the screen in Flutter.
BMaterialApp provides material design theme, navigation, localization, and other app-wide features needed by widgets like Scaffold.
CMaterialApp automatically creates all screens and routes without developer input.
DMaterialApp replaces the need for Scaffold and other layout widgets.
Attempts:
2 left
💡 Hint

Think about what MaterialApp offers beyond just showing widgets.

Practice

(1/5)
1. What is the main purpose of the MaterialApp widget in a Flutter app?
easy
A. It sets up the app's theme, routes, and basic navigation structure.
B. It creates the main content area of the screen.
C. It displays a button on the screen.
D. It manages the app's database connections.

Solution

  1. Step 1: Understand MaterialApp role

    The MaterialApp widget is designed to provide app-wide settings like theme, navigation routes, and localization.
  2. Step 2: Differentiate from Scaffold

    Scaffold handles screen layout, not app-wide settings.
  3. Final Answer:

    It sets up the app's theme, routes, and basic navigation structure. -> Option A
  4. Quick Check:

    MaterialApp = app-wide setup [OK]
Hint: MaterialApp wraps the whole app, setting theme and navigation [OK]
Common Mistakes:
  • Confusing Scaffold with MaterialApp
  • Thinking MaterialApp creates screen content
  • Assuming MaterialApp manages buttons directly
2. Which of the following is the correct way to create a basic Scaffold with an AppBar titled "Home"?
easy
A. Scaffold(appBar: AppBar(title: Text('Home')))
B. Scaffold(AppBar: Text('Home'))
C. Scaffold(appBar: Text('Home'))
D. Scaffold(title: AppBar('Home'))

Solution

  1. Step 1: Check Scaffold syntax

    The Scaffold widget requires an appBar property set to an AppBar widget.
  2. Step 2: Verify AppBar usage

    AppBar takes a title property which is a Text widget.
  3. Final Answer:

    Scaffold(appBar: AppBar(title: Text('Home'))) -> Option A
  4. Quick Check:

    Scaffold appBar needs AppBar widget with title [OK]
Hint: Scaffold's appBar expects an AppBar widget with a Text title [OK]
Common Mistakes:
  • Using capitalized property names incorrectly
  • Passing Text directly to appBar
  • Confusing property names like title vs appBar
3. What will be displayed when running this Flutter code?
MaterialApp(
  home: Scaffold(
    appBar: AppBar(title: Text('Welcome')),
    body: Center(child: Text('Hello World')),
  ),
)
medium
A. A blank screen with no content.
B. Only the text 'Hello World' without an app bar.
C. An error because MaterialApp needs a title property.
D. A screen with an app bar titled 'Welcome' and centered text 'Hello World'.

Solution

  1. Step 1: Analyze MaterialApp and Scaffold usage

    The MaterialApp wraps the app and sets home to a Scaffold widget.
  2. Step 2: Check Scaffold content

    The Scaffold has an AppBar with title 'Welcome' and a body with centered text 'Hello World'.
  3. Final Answer:

    A screen with an app bar titled 'Welcome' and centered text 'Hello World'. -> Option D
  4. Quick Check:

    MaterialApp + Scaffold shows app bar and body [OK]
Hint: MaterialApp home shows Scaffold with app bar and body [OK]
Common Mistakes:
  • Thinking MaterialApp requires a title property
  • Ignoring Scaffold's appBar and body
  • Assuming no UI appears without explicit runApp
4. Identify the error in this Flutter code snippet:
MaterialApp(
  home: Scaffold(
    appBar: AppBar(title: Text('My App')),
    body: Text('Hello'),
  ),
)
medium
A. Missing a comma after the Scaffold widget.
B. AppBar title must be a String, not a Text widget.
C. The body should be wrapped in a widget like Center or Padding for proper layout.
D. MaterialApp cannot have a Scaffold as home.

Solution

  1. Step 1: Check Scaffold body usage

    The body is a Text widget directly, which works but may cause layout issues without padding or centering.
  2. Step 2: Identify best practice

    Wrapping Text in Center or Padding improves UI and avoids layout warnings.
  3. Final Answer:

    The body should be wrapped in a widget like Center or Padding for proper layout. -> Option C
  4. Quick Check:

    Scaffold body needs layout widget for good UI [OK]
Hint: Wrap body Text in Center or Padding to avoid layout issues [OK]
Common Mistakes:
  • Thinking AppBar title must be String, not Text
  • Believing MaterialApp can't have Scaffold as home
  • Ignoring layout best practices for body content
5. You want to create a Flutter app with a blue app bar titled "Dashboard" and a body showing a centered button labeled "Click Me". Which code snippet correctly achieves this?
hard
A. MaterialApp(home: Scaffold(appBar: AppBar(title: 'Dashboard', backgroundColor: Colors.blue), body: Center(child: Button('Click Me'))))
B. MaterialApp(home: Scaffold(appBar: AppBar(title: Text('Dashboard'), backgroundColor: Colors.blue), body: Center(child: ElevatedButton(onPressed: () {}, child: Text('Click Me')))))
C. MaterialApp(home: Scaffold(appBar: AppBar(title: Text('Dashboard'), color: Colors.blue), body: Button('Click Me')))
D. MaterialApp(home: Scaffold(appBar: AppBar(title: Text('Dashboard'), backgroundColor: Colors.blue), body: Center(child: ElevatedButton(child: Text('Click Me')))))

Solution

  1. Step 1: Set AppBar title and color correctly

    The AppBar title must be a Text widget and background color set with backgroundColor.
  2. Step 2: Use ElevatedButton with onPressed and child

    The button must be an ElevatedButton with an onPressed callback and a Text child.
  3. Step 3: Wrap button in Center for alignment

    Center widget ensures the button is centered in the body.
  4. Final Answer:

    MaterialApp(home: Scaffold(appBar: AppBar(title: Text('Dashboard'), backgroundColor: Colors.blue), body: Center(child: ElevatedButton(onPressed: () {}, child: Text('Click Me'))))) -> Option B
  5. Quick Check:

    AppBar color + ElevatedButton with onPressed + Center [OK]
Hint: Use backgroundColor for AppBar and ElevatedButton with onPressed [OK]
Common Mistakes:
  • Using 'color' instead of 'backgroundColor' for AppBar
  • Omitting onPressed in ElevatedButton
  • Passing title as String instead of Text widget
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