onChanged property
Called when the checkbox should change between selected and de-selected states.
When the checkbox is tapped, then the onChanged callback, if set, will be
applied to !selected
(see selectedStyle
).
The checkbox passes the new value to the callback but does not actually change state until the parent widget rebuilds the checkbox with the new value.
The callback provided to onChanged should update the state of the parent StatefulWidget using the State.setState method, so that the parent gets rebuilt.
{@tool snippet}
A StatefulWidget that illustrates use of onSelected in an Checkbox.
class Wood extends StatefulWidget {
const Wood({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => WoodState();
}
class WoodState extends State<Wood> {
bool _useChisel = false;
@override
Widget build(BuildContext context) {
return Checkbox(
label: const Text('Use Chisel'),
selected: _useChisel,
onSelected: (bool newValue) {
setState(() {
_useChisel = newValue;
});
},
);
}
}
{@end-tool}
Implementation
final ValueChanged<bool>? onChanged;