Files
flisp_app/lib/widgets/todo_widget.dart
2025-11-06 23:52:20 +08:00

201 lines
5.5 KiB
Dart

import 'package:flisp_app/models/todo.dart';
import 'package:flisp_app/utils/date_utils.dart';
import 'package:flisp_app/widgets/common.dart';
import 'package:flutter/material.dart';
import 'package:toggle_switch/toggle_switch.dart';
// 统计卡片
Widget buildStatsCard(List<TodoItem> todos) {
int totalCount = todos.length;
int activeCount = todos.where((todo) => !todo.isCompleted).length;
int completedCount = todos.where((todo) => todo.isCompleted).length;
return buildCard(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_buildStatItem('总计', totalCount, Colors.blue),
_buildStatItem('待完成', activeCount, Colors.orange),
_buildStatItem('已完成', completedCount, Colors.green),
],
),
);
}
Widget _buildStatItem(String label, int count, Color color) {
return Column(
children: [
Text(
count.toString(),
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: color,
),
),
const SizedBox(height: 4),
Text(label, style: const TextStyle(fontSize: 12, color: Colors.grey)),
],
);
}
// Tab页
Widget buildTabs({
required TodoTab currentTab,
required ValueChanged<TodoTab> onTabChanged,
}) {
return Container(
padding: EdgeInsets.all(8),
child: ToggleSwitch(
minWidth: 90.0,
minHeight: 40.0,
initialLabelIndex: TodoTab.values.indexOf(currentTab),
totalSwitches: TodoTab.values.length,
labels: TodoTab.values.map((e) => e.label).toList(),
activeBgColor: [Colors.orange.shade600],
activeFgColor: Colors.white,
inactiveBgColor: Colors.grey.shade200,
inactiveFgColor: Colors.grey.shade700,
cornerRadius: 12.0,
customTextStyles: [TextStyle(fontSize: 12, fontWeight: FontWeight.w500)],
onToggle: (index) {
if (index != null) {
onTabChanged(TodoTab.values[index]);
}
},
),
);
}
Widget buildTodoList({
required List<TodoItem> todos,
required ValueChanged<String> onToggleTodo,
required ValueChanged<TodoItem> onEditTodo
}) {
return ListView.separated(
itemCount: todos.length,
separatorBuilder: (context, index) => SizedBox(height: 8),
itemBuilder: (context, index) {
final todo = todos[index];
return _buildTodoItem(
todo: todo,
onToggle: onToggleTodo,
onEdit: onEditTodo
);
},
);
}
Widget _buildTodoItem({
required TodoItem todo,
required ValueChanged<String> onToggle,
required ValueChanged<TodoItem> onEdit
}) {
return buildCard(
child: ListTile(
leading: Checkbox(
value: todo.isCompleted,
onChanged: (value) => onToggle(todo.id),
),
title: _buildTodoTitle(todo),
subtitle: _buildTodoSubtitle(todo),
trailing: _buildPriorityBadge(todo.priority),
onTap: () => onEdit(todo),
// onLongPress: () => onShowOptions(todo),
),
);
}
Widget _buildTodoTitle(TodoItem todo) {
return Text(
todo.title,
style: TextStyle(
decoration: todo.isCompleted ? TextDecoration.lineThrough : null,
color: todo.isCompleted ? Colors.grey : null,
fontWeight: FontWeight.w500,
),
);
}
// 构建待办事项副标题
Widget? _buildTodoSubtitle(TodoItem todo) {
final isOverdue =
todo.dueDate != null &&
todo.dueDate!.isBefore(DateTime.now()) &&
!todo.isCompleted;
final hasContent =
todo.description?.isNotEmpty == true || todo.dueDate != null;
if (!hasContent) return null;
return Wrap(
direction: Axis.vertical,
spacing: 5,
children: [
if (todo.description?.isNotEmpty == true)
Text(
todo.description!,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 12, color: Colors.grey.shade600),
),
if (todo.dueDate != null)
Text(
'截止: ${formatDate(todo.dueDate!)}',
style: TextStyle(
fontSize: 11,
color: isOverdue ? Colors.red : Colors.grey,
fontWeight: isOverdue ? FontWeight.bold : FontWeight.normal,
),
),
],
);
}
// 构建优先级徽章
Widget _buildPriorityBadge(TodoPriority priority) {
return Chip(
label: Text(priority.label),
backgroundColor: priority.color,
labelStyle: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.bold,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
side: BorderSide(color: Colors.white),
),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
visualDensity: VisualDensity.compact,
);
}
// 空状态
Widget buildEmptyState(TodoTab currentFilter) {
final messages = {
TodoTab.all: '📝 还没有待办事项\n点击➕号添加第一个任务吧~',
TodoTab.active: '🎯 没有待完成的任务\n享受轻松时光吧!✨',
TodoTab.completed: '🎉 还没有完成的任务\n加油哦!💪',
TodoTab.today: '📅 今天没有安排任务\n好好放松一下吧~😊',
};
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.checklist, size: 80, color: Colors.orange.shade600),
const SizedBox(height: 20),
Text(
messages[currentFilter] ?? '暂无数据',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16, color: Colors.orange.shade600),
),
],
),
);
}