0

whenever I create void function the build method of stateless widget is not called due to which further methods are also not called. Kindly guide with the possible solution.

    import 'package:flutter/material.dart';
class TaskTile extends StatefulWidget {
  @override
  _TaskTileState createState() => _TaskTileState();
}


class _TaskTileState extends State<TaskTile> {
  bool isChecked= true;

   void checkboxCallback (checkboxState){
    setState(() {
      isChecked=che ckboxState;
    });

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text('This is my task.',
        style: TextStyle(
        decoration: isChecked ? TextDecoration.lineThrough : null,
    ),),
      trailing: TaskCheckBox(isChecked,checkBoxCallback),
    );
  }
}

class TaskCheckBox extends StatelessWidget {

  final bool checkboxState;
  final Function toggleCheckBoxState;

  TaskCheckBox (this.checkboxState, this.toggleCheckBoxState);

  @override
  Widget build(BuildContext context) {
    return Checkbox(
      activeColor: Colors.blueAccent,
        value: checkboxState,
        onChanged:
        });
  }
}
Usama Bin Tahir
  • 143
  • 1
  • 11
  • The code you posted does not even compile, so it cannot possibly call anything. Please either describe what your current problem is with the code as posted, or post the code that has the problems you describe. – nvoigt Aug 25 '21 at 08:07
  • Its a part of todo app and here I am creating the checkbox in this code. That when the checkbox is true it marks the text with line and when the value is false it checkbox is unchecked and text is without the line through. By doing this creating the void function it donot implements the other methods. Its a part of checkbox only not the whole code of an app. – Usama Bin Tahir Aug 25 '21 at 08:16

2 Answers2

0

Your toggleCheckBoxState variable needs to be of type:

final ValueChanged<bool?> toggleCheckBoxState;

Then you can say:

onChanged: toggleCheckBoxState,

Then you only need to figure out what to do if the checkbox has a null value, which your code right now has not. Maybe find a default in your checkboxCallback?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

You can try calling the function with an anonymous function

Widget build(BuildContext context) {
    return Checkbox(
      activeColor: Colors.blueAccent,
        value: checkboxState,
        onChanged: ()=>toggleCheckBoxState()
        });   }

also instead of

Function toggleCheckBoxState

you can use

VoidCallback toggleCheckBoxState

for defining any callback

Niyas Ali
  • 203
  • 2
  • 9