skip to Main Content
class TodoItem extends StatelessWidget {

  final Todo todo;
  const TodoItem({super.key, required this.todo});

  
  @override
  Widget build(BuildContext context) {

    return  ListTile(
      contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
      selectedTileColor: Colors.red,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(15)
      ),
      tileColor: Colors.grey,
      leading: **IconButton(
          color: Colors.black,
          iconSize: 18,
          icon: Icon( 
            todo.isDone ? Icons.check_box : Icons.check_box_outline_blank
            ),
          onPressed: () {
              todo.isDone = !todo.isDone;
          },
        ),**
      title:  Text(
        "${todo.body}",
        style: const TextStyle(
          fontSize: 25,
          color: Colors.black,
          decoration: TextDecoration.lineThrough,
        ),
      ),
      subtitle: const Text(
        'date',
        style: TextStyle(
          color: Colors.black,
          fontSize: 15,
        ),
      ),
      trailing: Container(
        height: 30,
        width: 30,
        decoration: BoxDecoration(
          color: Colors.red,
          borderRadius: BorderRadius.circular(5),
        ),
        child: IconButton(
          color: Colors.white,
          iconSize: 18,
          icon: const Icon(Icons.delete),
          onPressed: () {},
        ),
      ),
    );
  }
}

This is the class but the problem is:


leading: IconButton(
          color: Colors.black,
          iconSize: 18,
          icon: Icon( 
            todo.isDone ? Icons.check_box : Icons.check_box_outline_blank // here is just giving me the checkbox
            ),
          onPressed: () {
              todo.isDone = !todo.isDone;
          },
        ),

Didn’t try anything eles … i know another way that can work but it’s just an icon

2

Answers


  1. Chosen as BEST ANSWER

    i found the problem: first i have to use a stateful widget and in the onPressed function i need to use the setstate function


  2. You can use setState((){}) after changing the value of todo.isDone by making the widget StatefulWidget or use can declare value of isDone in Todo as ValueNotifier of type bool and wrap the ListTIle in ValueListenableBuilder to get the UI re-render and changes get reflected.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search