Component - Checkbox and Switch
This UI component shows two common interactive controls: a checkbox and a switch. Both let users toggle between two states, like turning something on or off.
This UI component shows two common interactive controls: a checkbox and a switch. Both let users toggle between two states, like turning something on or off.
Scaffold
├─ AppBar
│ └─ Text
└─ Padding
└─ Column
├─ Row
│ ├─ Checkbox
│ └─ Text
└─ Row
├─ Switch
└─ TextCheckbox 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