List<Map<String, dynamic>> todoItems = [
{'title': '買牛奶', 'isDone': false},
{'title': '寫作業', 'isDone': false},
{'title': '運動', 'isDone': false},
];
•每個 ListTile 的 onTap 改成切換完成狀態:
onTap: () {
setState(() {
todoItems[index]['isDone'] = !todoItems[index]['isDone'];
});
}
•完成的項目文字加刪除線:
style: TextStyle(
decoration: todoItems[index]['isDone'] ? TextDecoration.lineThrough : null,
)
ListView.builder(
itemCount: todoItems.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
todoItems[index]['title'],
style: TextStyle(
decoration: todoItems[index]['isDone'] ? TextDecoration.lineThrough : null,
),
),
onTap: () {
setState(() {
todoItems[index]['isDone'] = !todoItems[index]['isDone'];
});
},
);
},
)
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
String newTask = '';
return AlertDialog(
title: Text('新增待辦事項'),
content: TextField(
onChanged: (value) => newTask = value,
),
actions: [
TextButton(
onPressed: () {
setState(() {
todoItems.add({'title': newTask, 'isDone': false});
});
Navigator.of(context).pop();
},
child: Text('新增'),
),
],
);
},
);
},
child: Icon(Icons.add),
)