CheckBox not getting ticked

105

Solution 1

Try below Code hope its help to you.

Declare bool variable

bool isChecked = false;

Your Widget

   ListTile(
        title: Text(
          'This is task',
          style: TextStyle(
              decoration: isChecked ? TextDecoration.lineThrough : null),
        ),
        trailing: Checkbox(
          checkColor: Colors.white,
          value: isChecked,
          onChanged: (bool? value) {
            setState(() {
              isChecked = value!;
            });
          },
        ),
      ),

Your Result before check-> enter image description here

Your Result after check-> enter image description here

Solution 2

You can call setState() like this:

setState(() {
    isChecked = !isChecked;
});

Solution 3

The toggleCheckBoxState is a function without parameter, which is why the state is not changing in the checkBoxCallback as the parameter checkBoxState did not have a value.

To fix it, you should change the TaskCheckBox as follow:

 class TaskCheckBox extends StatefulWidget {
      late final bool checkboxState;
      final Function(bool?) toggleCheckBoxState;
      TaskCheckBox(this.checkboxState, this.toggleCheckBoxState);
    
      @override
      State<TaskCheckBox> createState() => _TaskCheckBoxState();
    }
    
  class _TaskCheckBoxState extends State<TaskCheckBox> {
      @override
      Widget build(BuildContext context) {
        return Checkbox(
        value:(widget.checkboxState),
        activeColor:Colors.lightBlueAccent,
          onChanged: (bool? value) {
            widget.toggleCheckBoxState(value);
          },
        );
      }
    }
Share:
105
Karthik KK
Author by

Karthik KK

Updated on January 01, 2023

Comments

  • Karthik KK
    Karthik KK 10 months

    I am making a List App which a list of items followed by a checkBox .When i run the app the checkBox dosent seem to change the state .It is inactive . I am using the flutter sdk version 2.12.0 with null safety.

    Here is my Code

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    class TaskTile extends StatefulWidget {
       const TaskTile({ required this.text});
      final Text text;
      @override
      State<TaskTile> createState() => _TaskTileState();
    }
    class _TaskTileState extends State<TaskTile> {
      bool isChecked = true;
      void checkBoxCallBack(bool checkBoxState)
      {
        setState((){
          isChecked = checkBoxState;
        });
      }
      @override
      Widget build(BuildContext context) {
        return ListTile(
          title: Text('This is task',
          style:TextStyle(
            decoration:  isChecked ? TextDecoration.lineThrough:null),
          ),
          trailing:  TaskCheckBox(isChecked, checkBoxCallBack),
        );
      }
    }
    class TaskCheckBox   extends StatefulWidget {
      late final bool checkboxState;
      final Function toggleCheckBoxState;
      TaskCheckBox(this.checkboxState, this.toggleCheckBoxState);
    
      @override
      State<TaskCheckBox> createState() => _TaskCheckBoxState();
    }
    
    class _TaskCheckBoxState extends State<TaskCheckBox> {
      @override
      Widget build(BuildContext context) {
        return Checkbox(
        value:(widget.checkboxState),
        activeColor:Colors.lightBlueAccent,
          onChanged: (bool? value) {
          widget.toggleCheckBoxState;
          },
        );
      }
    }
    
  • Karthik KK
    Karthik KK about 2 years
    Code is working :). Thank you .But I didn't understand how the state gets changed from false to true.
  • Ravindra S. Patil
    Ravindra S. Patil about 2 years
    Welcome, if your problem is solve accept and upvote my answer, Thank you, because your state is not constant it change like checked or unchecked, refer bool here