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(),
),
);
}
}
在此处输入图像描述
更改属性中的复选框内部不使用复选框回调方法。你知道我在这里做错了什么吗?根据我的在线教练的说法,它正在开发她的旧版本飞镖,但现在似乎在最新版本中不起作用。
使用onChanged: checkboxCallback()
将在构建框架时调用该方法,您可以使用onChanged: checkboxCallback,
,当项目发生更改时,它将得到调用。