CheckBox not getting ticked

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

here is solutions:

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

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-> CheckBox not getting ticked

Your Result after check-> CheckBox not getting ticked

Solution 2

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

Solution 3

You can call setState() like this:

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply