Bird
Raised Fist0
Fluttermobile~20 mins

Checkbox and Switch in Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Settings Toggle Screen
A simple settings screen where users can toggle options using checkboxes and switches.
Target UI
Settings Toggle Screen

[ ] Enable Notifications
( ) Dark Mode

[Save Button]
Add a Checkbox labeled 'Enable Notifications' that toggles a boolean value.
Add a Switch labeled 'Dark Mode' that toggles a boolean value.
Add a Save button that shows a SnackBar confirming the current settings.
Starter Code
Flutter
import 'package:flutter/material.dart';

class SettingsToggleScreen extends StatefulWidget {
  @override
  State<SettingsToggleScreen> createState() => _SettingsToggleScreenState();
}

class _SettingsToggleScreenState extends State<SettingsToggleScreen> {
  bool notificationsEnabled = false;
  bool darkModeEnabled = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Settings Toggle Screen')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            // TODO: Add Checkbox for Enable Notifications
            // TODO: Add Switch for Dark Mode
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                // TODO: Show SnackBar with current settings
              },
              child: Text('Save'),
            ),
          ],
        ),
      ),
    );
  }
}
Task 1
Task 2
Task 3
Solution
Flutter
import 'package:flutter/material.dart';

class SettingsToggleScreen extends StatefulWidget {
  @override
  State<SettingsToggleScreen> createState() => _SettingsToggleScreenState();
}

class _SettingsToggleScreenState extends State<SettingsToggleScreen> {
  bool notificationsEnabled = false;
  bool darkModeEnabled = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Settings Toggle Screen')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            CheckboxListTile(
              title: Text('Enable Notifications'),
              value: notificationsEnabled,
              onChanged: (bool? value) {
                setState(() {
                  notificationsEnabled = value ?? false;
                });
              },
            ),
            SwitchListTile(
              title: Text('Dark Mode'),
              value: darkModeEnabled,
              onChanged: (bool value) {
                setState(() {
                  darkModeEnabled = value;
                });
              },
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                final snackBar = SnackBar(
                  content: Text('Notifications: ${notificationsEnabled ? 'On' : 'Off'}, Dark Mode: ${darkModeEnabled ? 'On' : 'Off'}'),
                );
                ScaffoldMessenger.of(context).showSnackBar(snackBar);
              },
              child: Text('Save'),
            ),
          ],
        ),
      ),
    );
  }
}

We use CheckboxListTile and SwitchListTile widgets because they combine the toggle control with a label, making the UI clear and accessible.

Each toggle updates its own boolean state variable inside setState() to refresh the UI.

The Save button shows a SnackBar that confirms the current toggle states, giving immediate feedback to the user.

Final Result
Completed Screen
Settings Toggle Screen

[✔] Enable Notifications
(ON) Dark Mode

[ Save ]
Tapping the checkbox toggles notifications on or off.
Tapping the switch toggles dark mode on or off.
Pressing Save shows a small message at the bottom confirming the current settings.
Stretch Goal
Add a reset button that sets both toggles back to off.
💡 Hint
Add another ElevatedButton labeled 'Reset' that sets notificationsEnabled and darkModeEnabled to false inside setState.

Practice

(1/5)
1. What is the main difference between a Checkbox and a Switch in Flutter?
easy
A. Checkbox requires a controller; Switch does not.
B. Checkbox allows multiple selections; Switch is for a single on/off choice.
C. Checkbox changes color automatically; Switch does not.
D. Checkbox is only for iOS; Switch is only for Android.

Solution

  1. Step 1: Understand Checkbox behavior

    A Checkbox lets users select multiple options independently.
  2. Step 2: Understand Switch behavior

    A Switch is a toggle for a single on/off setting, not multiple selections.
  3. Final Answer:

    Checkbox allows multiple selections; Switch is for a single on/off choice. -> Option B
  4. Quick Check:

    Checkbox = multiple, Switch = single toggle [OK]
Hint: Checkbox = multiple choices, Switch = single toggle [OK]
Common Mistakes:
  • Thinking Switch can select multiple options
  • Confusing platform-specific behavior
  • Assuming Checkbox needs a controller
2. Which of the following is the correct way to create a Checkbox widget with a boolean value isChecked and update it on change?
easy
A. Checkbox(checked: isChecked, onChange: (val) => setState(() => isChecked = val))
B. Checkbox(value: isChecked, onChanged: (val) { isChecked = val })
C. Checkbox(value: isChecked, onChanged: (val) => isChecked = val)
D. Checkbox(value: isChecked, onChanged: (val) => setState(() => isChecked = val!))

Solution

  1. Step 1: Check property names

    The correct property for Checkbox state is value and the callback is onChanged.
  2. Step 2: Use setState to update UI

    Inside onChanged, use setState to update isChecked with the new value.
  3. Final Answer:

    Checkbox(value: isChecked, onChanged: (val) => setState(() => isChecked = val!)) -> Option D
  4. Quick Check:

    Use value and onChanged with setState [OK]
Hint: Use value and onChanged with setState to update [OK]
Common Mistakes:
  • Missing setState causing UI not to update
  • Using wrong property names like checked or onChange
  • Not unwrapping nullable bool with val!
3. What will be the output behavior of this Flutter code snippet?
bool isSwitched = false;

Switch(
  value: isSwitched,
  onChanged: (value) {
    isSwitched = value;
  },
)
medium
A. The switch toggles visually and updates the UI correctly.
B. The code causes a compile-time error due to missing setState.
C. The switch does not toggle visually because setState is missing.
D. The switch toggles but the value never changes.

Solution

  1. Step 1: Analyze state update

    The variable isSwitched is updated but not inside setState.
  2. Step 2: Understand UI update mechanism

    Without setState, Flutter does not rebuild the widget, so UI won't reflect changes.
  3. Final Answer:

    The switch does not toggle visually because setState is missing. -> Option C
  4. Quick Check:

    Missing setState means no UI update [OK]
Hint: Always wrap state changes in setState for UI update [OK]
Common Mistakes:
  • Assuming variable change updates UI automatically
  • Confusing compile error with runtime behavior
  • Ignoring setState importance
4. Identify the error in this Flutter code for a Checkbox:
bool checked = false;

Checkbox(
  value: checked,
  onChanged: (bool val) {
    checked = val;
  },
)
medium
A. Missing setState call to update the UI.
B. Incorrect type for onChanged parameter.
C. Checkbox widget requires a controller.
D. Value property should be named checked.

Solution

  1. Step 1: Check state update method

    The variable checked is updated but not inside setState.
  2. Step 2: Understand Flutter UI update

    Without setState, the UI will not rebuild to reflect the new value.
  3. Final Answer:

    Missing setState call to update the UI. -> Option A
  4. Quick Check:

    State changes need setState for UI refresh [OK]
Hint: Use setState to refresh UI after state changes [OK]
Common Mistakes:
  • Forgetting setState causes no UI update
  • Thinking Checkbox needs a controller
  • Confusing parameter types
5. You want to create a settings page with two options: "Enable Notifications" (Switch) and "Accept Terms" (Checkbox). How should you manage their states to update the UI correctly when toggled?
hard
A. Use two boolean variables and update each inside setState when toggled.
B. Use a single boolean variable for both widgets and update it in setState.
C. Use final variables since the UI does not need to change after initial build.
D. Use global variables outside the widget and update them without setState.

Solution

  1. Step 1: Identify separate states

    Each widget needs its own boolean variable to track its state independently.
  2. Step 2: Update states with setState

    Wrap each state change inside setState to rebuild the UI correctly.
  3. Final Answer:

    Use two boolean variables and update each inside setState when toggled. -> Option A
  4. Quick Check:

    Separate booleans + setState = correct UI update [OK]
Hint: Use separate booleans and setState for each toggle [OK]
Common Mistakes:
  • Using one variable for multiple toggles
  • Not using setState causing UI freeze
  • Using final variables preventing 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