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]
Settings Toggle Screen [ ] Enable Notifications ( ) Dark Mode [Save Button]
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'), ), ], ), ), ); } }
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.
Settings Toggle Screen [✔] Enable Notifications (ON) Dark Mode [ Save ]
Checkbox and a Switch in Flutter?Checkbox widget with a boolean value isChecked and update it on change?value and the callback is onChanged.onChanged, use setState to update isChecked with the new value.bool isSwitched = false;
Switch(
value: isSwitched,
onChanged: (value) {
isSwitched = value;
},
)isSwitched is updated but not inside setState.setState, Flutter does not rebuild the widget, so UI won't reflect changes.Checkbox:
bool checked = false;
Checkbox(
value: checked,
onChanged: (bool val) {
checked = val;
},
)checked is updated but not inside setState.setState, the UI will not rebuild to reflect the new value.setState to rebuild the UI correctly.Pass the test and this pattern moves unknown