Checkboxes and switches let users choose options easily by tapping. They show if something is on or off.
Checkbox and Switch in Flutter
Checkbox(
value: isChecked,
onChanged: (bool? newValue) {
setState(() {
isChecked = newValue!;
});
},
)
Switch(
value: isSwitchedOn,
onChanged: (bool newValue) {
setState(() {
isSwitchedOn = newValue;
});
},
)Checkbox uses a nullable bool in onChanged because it can be tristate (true, false, null).
Switch is a simple on/off toggle with a non-nullable bool.
Checkbox( value: true, onChanged: null, )
Switch( value: false, onChanged: (val) => print('Switched: $val'), )
Checkbox(
value: isChecked,
onChanged: (val) {
setState(() {
isChecked = val!;
});
},
)Switch(
value: isSwitchedOn,
onChanged: (val) {
setState(() {
isSwitchedOn = val;
});
},
)This app shows a checkbox and a switch with labels. When you tap them, their state changes and the text below updates to show the current state.
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 isChecked = false; bool isSwitchedOn = false; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Checkbox and Switch Example')), body: Padding( padding: const EdgeInsets.all(20), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Row( children: [ Checkbox( value: isChecked, onChanged: (bool? value) { setState(() { isChecked = value!; }); }, ), const Text('Accept Terms'), ], ), const SizedBox(height: 20), Row( children: [ Switch( value: isSwitchedOn, onChanged: (bool value) { setState(() { isSwitchedOn = value; }); }, ), const Text('Enable Notifications'), ], ), const SizedBox(height: 40), Text('Checkbox is: ${isChecked ? 'Checked' : 'Unchecked'}'), Text('Switch is: ${isSwitchedOn ? 'On' : 'Off'}'), ], ), ), ), ); } }
Always wrap Checkbox and Switch in a stateful widget to update UI on changes.
Use labels next to controls for better accessibility and clarity.
Checkbox can have a tristate mode (true, false, null) if needed by setting tristate: true.
Checkbox and Switch let users toggle options on or off.
Checkbox can select multiple options; Switch is for simple on/off settings.
Use setState to update the UI when the user changes the value.
Practice
Checkbox and a Switch in Flutter?Solution
Step 1: Understand Checkbox behavior
A Checkbox lets users select multiple options independently.Step 2: Understand Switch behavior
A Switch is a toggle for a single on/off setting, not multiple selections.Final Answer:
Checkbox allows multiple selections; Switch is for a single on/off choice. -> Option BQuick Check:
Checkbox = multiple, Switch = single toggle [OK]
- Thinking Switch can select multiple options
- Confusing platform-specific behavior
- Assuming Checkbox needs a controller
Checkbox widget with a boolean value isChecked and update it on change?Solution
Step 1: Check property names
The correct property for Checkbox state isvalueand the callback isonChanged.Step 2: Use setState to update UI
InsideonChanged, usesetStateto updateisCheckedwith the new value.Final Answer:
Checkbox(value: isChecked, onChanged: (val) => setState(() => isChecked = val!)) -> Option DQuick Check:
Use value and onChanged with setState [OK]
- Missing setState causing UI not to update
- Using wrong property names like checked or onChange
- Not unwrapping nullable bool with val!
bool isSwitched = false;
Switch(
value: isSwitched,
onChanged: (value) {
isSwitched = value;
},
)Solution
Step 1: Analyze state update
The variableisSwitchedis updated but not insidesetState.Step 2: Understand UI update mechanism
WithoutsetState, Flutter does not rebuild the widget, so UI won't reflect changes.Final Answer:
The switch does not toggle visually because setState is missing. -> Option CQuick Check:
Missing setState means no UI update [OK]
- Assuming variable change updates UI automatically
- Confusing compile error with runtime behavior
- Ignoring setState importance
Checkbox:
bool checked = false;
Checkbox(
value: checked,
onChanged: (bool val) {
checked = val;
},
)Solution
Step 1: Check state update method
The variablecheckedis updated but not insidesetState.Step 2: Understand Flutter UI update
WithoutsetState, the UI will not rebuild to reflect the new value.Final Answer:
Missing setState call to update the UI. -> Option AQuick Check:
State changes need setState for UI refresh [OK]
- Forgetting setState causes no UI update
- Thinking Checkbox needs a controller
- Confusing parameter types
Solution
Step 1: Identify separate states
Each widget needs its own boolean variable to track its state independently.Step 2: Update states with setState
Wrap each state change insidesetStateto rebuild the UI correctly.Final Answer:
Use two boolean variables and update each inside setState when toggled. -> Option AQuick Check:
Separate booleans + setState = correct UI update [OK]
- Using one variable for multiple toggles
- Not using setState causing UI freeze
- Using final variables preventing changes
Ready to prove it?
Pass the test and this pattern moves unknown
