task_list.dart
import 'package:flutter/material.dart';
import 'package:todoey_app/widgets/task_tile.dart';
import 'package:todoey_app/models/task.dart';
class TasksList extends StatefulWidget {
@override
State<TasksList> createState() => _TasksListState();
}
class _TasksListState extends State<TasksList> {
List<Task> tasks = [
Task(name: 'Buy milk'),
Task(name: 'Buy eggs'),
Task(name: 'Buy bread'),
];
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
return TaskTile(
taskTitle: tasks[index].name,
isChecked: tasks[index].isDone,
checkboxCallback: (bool checkboxState) {
setState(() {
tasks[index].toggleDone();
});
});
},
itemCount: tasks.length,
);
}
}
task_tile.dart
import 'package:flutter/material.dart';
class TaskTile extends StatelessWidget {
late final bool isChecked;
late final String taskTitle;
final Function checkboxCallback;
TaskTile(
{required this.isChecked,
required this.taskTitle,
required this.checkboxCallback});
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
taskTitle,
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null),
),
trailing: Checkbox(
activeColor: Colors.lightBlueAccent,
value: isChecked,
onChanged: checkboxCallback(),
// onChanged: toggleCheckboxState(),
),
);
}
}
Inside the checkbox in on changed property is not working with the check box call back method. Do you know what i have done wrong here? As per my online instructor it was working on her older version of dart but seems it's not working now in the latest version.