Home > OS >  How can I solve a problem with the "Checkbox" icon?
How can I solve a problem with the "Checkbox" icon?

Time:05-14

i follow a german flutter tutorial. In this tutorial we build a simple To-Do list. I have the following problem:

https://github.com/simpleclub/startup_teens_flutter/blob/master/lib/stateless_widgets.dart

This is the link to the Github repo.The error is in line 35. What can I do?

CodePudding user response:

Try the below code:

 leading: Checkbox(
      onChanged: (bool? value){
        // your code for onChanged
      },
      value: false,
    ),

Here is fully working code

CodePudding user response:

I believe the error you have is something along the lines of

Error: Required named parameter 'onChanged' must be provided.

As mentioned, you have to provide an onChanged function to use the checkbox. You should also be using a variable to determine the value, and update that variable in the onChanged function.

class ToDoItem extends StatelessWidget {
  final String title;
  bool check = false; //Variable to monitor checkbox state
  
  ToDoItem(this.title); //I've removed const to allow check to be variable

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 22),
      child: ListTile(
        contentPadding: EdgeInsets.symmetric(vertical: 8.0),
        leading: Checkbox(
            value: check,   //Use check variable here
            onChanged: (bool? v) { //onChanged parameter here
              check = v ?? false;
            }),
        title: Text(
          title,
          style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w600, color: Colors.black54),
        ),
        trailing: Icon(Icons.delete_outline),
      ),
    );
  }
}

Note: You are currently using a StatelessWidget. However, you need a StatefulWidget to update the checkbox succesfully.

  • Related