oid _toggleDone(int index) {
setState(() {
_todoList[index]['done'] = !_todoList[index]['done'];
// 第十三天新增:依完成狀態排序
_todoList.sort((a, b) => a['done'] ? 1 : -1);
});
}
// 第十三天新增:ReorderableListView 替代 ListView.builder
// 目前只是框架,之後可實作拖曳排序
Expanded(
child: ListView.builder(
itemCount: _todoList.length,
itemBuilder: (context, index) {
final todo = _todoList[index];
return ListTile(
key: ValueKey(todo['task']),
title: Text(
todo['task'],
style: TextStyle(
decoration: todo['done']
? TextDecoration.lineThrough
: TextDecoration.none,
),
),
leading: GestureDetector(
onTap: () => _toggleDone(index),
child: Container(
width: 24,
height: 24,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: todo['done'] ? Colors.purple : Colors.grey,
width: 2,
),
),
child: todo['done']
? Icon(Icons.check, size: 16, color: Colors.purple)
: null,
),
),
trailing: IconButton(
icon: const Icon(Icons.delete, color: Colors.yellow),
onPressed: () => _deleteTask(index),
),
);
},
),
)
總結:
• 目標:完成任務自動排序功能,並為手動拖曳排序做準備
• 畫面效果:打勾後的任務會自動移到下方,未完成任務保持在上方,列表仍保持紫色主題與圓形打勾框,垃圾桶功能保持不變