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
i found the problem: first i have to use a stateful widget and in the onPressed function i need to use the setstate function
You can use
setState((){})
after changing the value oftodo.isDone
by making the widgetStatefulWidget
or use can declare value ofisDone
inTodo
asValueNotifier
of type bool and wrap theListTIle
inValueListenableBuilder
to get the UI re-render and changes get reflected.