新增標籤篩選功能
• 在任務列表上方增加一列標籤按鈕(工作、學習、個人、健康)
• 點擊標籤按鈕時,只顯示該標籤的任務,其他標籤任務隱藏
標籤互動顯示
• 任務的標籤顏色依照設定顯示(工作:灰色,學習:綠色,個人:藍色,健康:橘色)
• 未點選任何篩選時,顯示所有任務
維持已完成項目排序與框架
• 勾選完成的任務仍自動移到列表底部
• 每個任務仍保留長方形框,勾選後文字槓掉,框顏色維持
// 第十七天:新增標籤篩選按鈕
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_buildFilterButton('工作', Colors.grey),
_buildFilterButton('學習', Colors.green),
_buildFilterButton('個人', Colors.blue),
_buildFilterButton('健康', Colors.orange),
],
),
// 篩選按鈕生成函數
Widget _buildFilterButton(String label, Color color) {
final bool isSelected = _selectedFilter == label;
return ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: isSelected ? color : Colors.grey.shade300,
),
onPressed: () {
setState(() {
_selectedFilter = isSelected ? null : label;
});
},
child: Text(label),
);
}
// 顯示任務時依篩選條件過濾
ListView.builder(
itemCount: _todoList.length,
itemBuilder: (context, index) {
final item = _todoList[index];
if (_selectedFilter != null && item.label != _selectedFilter) {
return const SizedBox.shrink(); // 不顯示非篩選標籤
}
return ToDoItemWidget(
item: item,
onToggleDone: () => _toggleDone(index),
onDelete: () => _deleteTask(index),
);
},
),