Flutter: CheckboxTile tick mark not changing

Issue

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

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
        ),
      ),
    );
  }
}

Answered By – Raine Dale Holgado

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *