Files
flisp_app/lib/utils/todo_utils.dart
2025-11-21 14:58:11 +08:00

53 lines
1.6 KiB
Dart

import 'package:flisp_app/models/todo.dart';
import 'package:intl/intl.dart';
List<Todo> getActiveTodos(TodoTab currentTab, List<Todo> todos) {
switch (currentTab) {
case TodoTab.active:
return todos.where((todo) => !todo.isCompleted).toList();
case TodoTab.completed:
return todos.where((todo) => todo.isCompleted).toList();
case TodoTab.today:
final today = DateTime.now();
return todos
.where(
(todo) =>
todo.dueDate != null &&
todo.dueDate!.year == today.year &&
todo.dueDate!.month == today.month &&
todo.dueDate!.day == today.day,
)
.toList();
default:
return todos;
}
}
// 格式化截止日期
String formatDueDate(DateTime dueDate, bool isToday, bool isTomorrow) {
final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
final dueDateStart = DateTime(dueDate.year, dueDate.month, dueDate.day);
final difference = dueDateStart.difference(today);
// 处理过期情况
if (difference.inDays < 0) {
final daysPast = -difference.inDays;
return '已过期$daysPast天';
}
// 处理未来日期
if (difference.inDays == 0) return '今天截止';
if (difference.inDays == 1) return '明天截止';
if (difference.inDays == 2) return '后天截止';
if (difference.inDays < 7) return '${difference.inDays}天后截止';
return DateFormat("MM-dd").format(dueDate);
}
bool isSameDay(DateTime date1, DateTime date2) {
return date1.year == date2.year &&
date1.month == date2.month &&
date1.day == date2.day;
}