'Flutter: CheckboxTile tick mark not changing
I am trying to use Checkboxtile in Flutter. However, tapping on it is not changing the state of tick.
Here is the code:
bool checkedValue = false;
CheckboxListTile(
title: Text("title text"),
value: checkedValue,
onChanged: (newValue) {
setState(() {
checkedValue = newValue;
});
},
//onChanged: (newValue) { ... },
controlAffinity: ListTileControlAffinity.leading, // <-- leading Checkbox
),
Why it is not working?
Solution 1:[1]
This might work for you.
class CheckBoxCustom extends StatefulWidget {
@override
_CheckBoxCustomState createState() => _CheckBoxCustomState();
}
class _CheckBoxCustomState extends State<CheckBoxCustom> {
bool checkedValue = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: CheckboxListTile(
title: Text("title text"),
value: checkedValue,
onChanged: (newValue) {
setState(() {
checkedValue = newValue;
});
},
controlAffinity:
ListTileControlAffinity.leading, // <-- leading Checkbox
),
),
);
}
}
Solution 2:[2]
try this:
Create a bool i.e. bool isChecked= false;
Checkbox( activeColor: Colors.white30, checkColor: Colors.white, value: isCheck, tristate: false, onChanged: (bool isChecked) { setState(() { isCheck = isChecked; }); }, )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Raine Dale Holgado |
| Solution 2 | Aashutosh Poudel |
