Files
flisp_app/lib/widgets/todo_widget.dart
2025-11-08 22:06:16 +08:00

215 lines
6.0 KiB
Dart

import 'package:flisp_app/models/todo.dart';
import 'package:flisp_app/widgets/common.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:toggle_switch/toggle_switch.dart';
// 统计卡片
Widget buildStatsCard(List<Todo> 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<Todo> todos,
required ValueChanged<Todo> onToggleTodo,
required ValueChanged<Todo> 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 Todo todo,
required ValueChanged<Todo> onToggle,
required ValueChanged<Todo> onEdit,
}) {
return buildCard(
child: ListTile(
contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 0),
leading: SizedBox(
width: 24,
child: Checkbox(
value: todo.isCompleted,
onChanged: (value) => onToggle(todo),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
),
title: _buildTodoTitle(todo),
subtitle: _buildTodoSubtitle(todo),
trailing: _buildPriorityBadge(todo.priority),
onTap: () => onEdit(todo),
// onLongPress: () => onShowOptions(todo),
),
);
}
Widget _buildTodoTitle(Todo todo) {
return Text(
todo.title,
style: TextStyle(
decoration: todo.isCompleted ? TextDecoration.lineThrough : null,
color: todo.isCompleted ? Colors.grey : null,
fontSize: 16,
fontWeight: FontWeight.w500,
),
);
}
// 构建待办事项副标题
Widget? _buildTodoSubtitle(Todo todo) {
final isOverdue =
todo.dueDate != null &&
todo.dueDate!.isBefore(DateTime.now()) &&
!todo.isCompleted;
final hasContent = todo.content.isNotEmpty == true || todo.dueDate != null;
if (!hasContent) return null;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 4),
if (todo.content.isNotEmpty == true)
Text(
todo.content,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 14,
color: Colors.grey[700],
),
),
SizedBox(height: 8),
if (todo.dueDate != null)
Container(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: isOverdue ? Colors.red[50] : Colors.grey[50],
borderRadius: BorderRadius.circular(4),
border: Border.all(
color: isOverdue ? Colors.red[100]! : Colors.grey[300]!,
),
),
child: Text(
'截止: ${DateFormat("yyyy-MM-dd").format(todo.dueDate!)}',
style: TextStyle(
fontSize: 11,
color: isOverdue ? Colors.red[600] : Colors.grey[600],
fontWeight: isOverdue ? FontWeight.w600 : FontWeight.normal,
),
),
),
],
);
}
// 构建优先级徽章
Widget _buildPriorityBadge(TodoPriority priority) {
return Chip(
label: Text(priority.label),
backgroundColor: priority.color,
labelStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
side: BorderSide(color: Colors.white),
),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
visualDensity: VisualDensity.compact,
);
}
// 空状态
Widget buildEmptyState(TodoTab tab) {
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),
Text(
messages[tab] ?? '暂无数据',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.orange.shade600),
),
],
),
);
}