255 lines
6.8 KiB
Dart
255 lines
6.8 KiB
Dart
import 'package:awesome_dialog/awesome_dialog.dart';
|
|
import 'package:flisp_app/models/todo.dart';
|
|
import 'package:flisp_app/utils/todo_utils.dart';
|
|
import 'package:flisp_app/widgets/common.dart';
|
|
import 'package:flisp_app/widgets/todo_dialog.dart';
|
|
import 'package:flisp_app/widgets/todo_widget.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class TodoPage extends StatefulWidget {
|
|
final VoidCallback? onAddTodoPressed;
|
|
|
|
const TodoPage({super.key, this.onAddTodoPressed});
|
|
|
|
@override
|
|
State<TodoPage> createState() => TodoPageState();
|
|
}
|
|
|
|
class TodoPageState extends State<TodoPage> {
|
|
// 添加一个公共方法供外部调用
|
|
void showAddTodoDialog() {
|
|
_showAddTodoDialog();
|
|
}
|
|
|
|
// 待办事项列表
|
|
final List<TodoItem> _todos = [
|
|
// ... 原有的待办事项数据保持不变
|
|
];
|
|
|
|
TodoTab _currentTab = TodoTab.all;
|
|
final TextEditingController _titleController = TextEditingController();
|
|
final TextEditingController _descriptionController = TextEditingController();
|
|
DateTime? _selectedDueDate;
|
|
TodoPriority _selectedPriority = TodoPriority.medium;
|
|
String? _selectedCategory;
|
|
|
|
// 获取过滤后的待办事项
|
|
List<TodoItem> get _activeTodos {
|
|
return getActiveTodos(_currentTab, _todos);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return buildBody(
|
|
child: Column(
|
|
children: [
|
|
// 统计信息卡片
|
|
buildStatsCard(_todos),
|
|
// 选项卡
|
|
buildTabs(
|
|
currentTab: _currentTab,
|
|
onTabChanged: (value) {
|
|
setState(() {
|
|
_currentTab = value;
|
|
});
|
|
},
|
|
),
|
|
// 待办事项列表
|
|
Expanded(child: _buildActiveTodoList()),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildActiveTodoList() {
|
|
return _activeTodos.isEmpty
|
|
? buildEmptyState(_currentTab)
|
|
: buildTodoList(
|
|
todos: _activeTodos,
|
|
onToggleTodo: (todoId) {
|
|
setState(() {
|
|
_toggleTodo(todoId);
|
|
});
|
|
},
|
|
onEditTodo: (todo) {
|
|
_showEditTodoDialog(todo);
|
|
}
|
|
);
|
|
}
|
|
|
|
// 显示添加待办事项对话框
|
|
void _showAddTodoDialog() {
|
|
_resetForm();
|
|
|
|
showAwesomeDialog(
|
|
context: context,
|
|
body: _buildTodoDialogContent(isEditing: false),
|
|
onOk: () => _saveTodo(false, null),
|
|
onCancel: () => _resetForm(),
|
|
);
|
|
}
|
|
|
|
// 显示编辑待办事项对话框
|
|
void _showEditTodoDialog(TodoItem todo) {
|
|
_titleController.text = todo.title;
|
|
_descriptionController.text = todo.description ?? '';
|
|
_selectedDueDate = todo.dueDate;
|
|
_selectedPriority = todo.priority;
|
|
_selectedCategory = todo.category;
|
|
|
|
showAwesomeDialog(
|
|
context: context,
|
|
body: _buildTodoDialogContent(isEditing: true),
|
|
onOk: () => _saveTodo(true, todo),
|
|
onCancel: () => _resetForm(),
|
|
);
|
|
}
|
|
|
|
// 构建待办事项对话框 - 现在使用新的组件
|
|
Widget _buildTodoDialogContent({bool isEditing = false}) {
|
|
return TodoDialogContent(
|
|
isEditing: isEditing,
|
|
titleController: _titleController,
|
|
descriptionController: _descriptionController,
|
|
selectedDueDate: _selectedDueDate,
|
|
selectedPriority: _selectedPriority,
|
|
selectedCategory: _selectedCategory,
|
|
onDueDateChanged: (date) {
|
|
setState(() {
|
|
_selectedDueDate = date;
|
|
});
|
|
},
|
|
onPriorityChanged: (priority) {
|
|
setState(() {
|
|
_selectedPriority = priority;
|
|
});
|
|
},
|
|
onCategoryChanged: (category) {
|
|
setState(() {
|
|
_selectedCategory = category;
|
|
});
|
|
},
|
|
onClearDueDate: () {
|
|
setState(() {
|
|
_selectedDueDate = null;
|
|
});
|
|
// 重新显示对话框以更新状态
|
|
if (isEditing) {
|
|
_showEditTodoDialog(_todos.firstWhere((todo) =>
|
|
_titleController.text == todo.title &&
|
|
_descriptionController.text == (todo.description ?? '')));
|
|
} else {
|
|
_showAddTodoDialog();
|
|
}
|
|
},
|
|
onSelectDueDate: () => _selectDueDateInDialog(isEditing),
|
|
);
|
|
}
|
|
|
|
// 在对话框中选择日期
|
|
Future<void> _selectDueDateInDialog(bool isEditing) async {
|
|
final DateTime? picked = await showDatePicker(
|
|
context: context,
|
|
initialDate: DateTime.now(),
|
|
firstDate: DateTime.now(),
|
|
lastDate: DateTime(2100),
|
|
);
|
|
|
|
if (picked != null) {
|
|
setState(() {
|
|
_selectedDueDate = picked;
|
|
});
|
|
// 重新显示对话框以更新日期显示
|
|
if (isEditing) {
|
|
// 找到对应的待办事项重新显示编辑对话框
|
|
final todo = _todos.firstWhere((todo) =>
|
|
_titleController.text == todo.title &&
|
|
_descriptionController.text == (todo.description ?? ''));
|
|
_showEditTodoDialog(todo);
|
|
} else {
|
|
_showAddTodoDialog();
|
|
}
|
|
}
|
|
}
|
|
|
|
// 验证表单
|
|
bool _validateForm() {
|
|
return _titleController.text.trim().isNotEmpty;
|
|
}
|
|
|
|
// 重置表单
|
|
void _resetForm() {
|
|
_titleController.clear();
|
|
_descriptionController.clear();
|
|
_selectedDueDate = null;
|
|
_selectedPriority = TodoPriority.medium;
|
|
_selectedCategory = null;
|
|
}
|
|
|
|
// 保存待办事项
|
|
void _saveTodo(bool isEditing, TodoItem? todo) {
|
|
if (!_validateForm()) return;
|
|
|
|
final newTodo = TodoItem(
|
|
id: isEditing ? todo!.id : DateTime.now().millisecondsSinceEpoch.toString(),
|
|
title: _titleController.text.trim(),
|
|
description: _descriptionController.text.trim().isEmpty
|
|
? null
|
|
: _descriptionController.text.trim(),
|
|
dueDate: _selectedDueDate,
|
|
priority: _selectedPriority,
|
|
category: _selectedCategory,
|
|
);
|
|
|
|
setState(() {
|
|
if (isEditing) {
|
|
final index = _todos.indexWhere((t) => t.id == todo!.id);
|
|
if (index != -1) {
|
|
_todos[index] = newTodo.copyWith(
|
|
isCompleted: _todos[index].isCompleted,
|
|
);
|
|
}
|
|
} else {
|
|
_todos.insert(0, newTodo);
|
|
}
|
|
});
|
|
|
|
_resetForm();
|
|
|
|
// 显示成功提示
|
|
_showSuccessDialog(isEditing ? '更新成功' : '添加成功');
|
|
}
|
|
|
|
// 显示成功提示
|
|
void _showSuccessDialog(String message) {
|
|
AwesomeDialog(
|
|
context: context,
|
|
dialogType: DialogType.success,
|
|
animType: AnimType.scale,
|
|
title: message,
|
|
btnOkText: "好的",
|
|
btnOkColor: Colors.green,
|
|
btnOkOnPress: () {},
|
|
autoHide: Duration(seconds: 2),
|
|
).show();
|
|
}
|
|
|
|
// 切换待办事项完成状态
|
|
void _toggleTodo(String id) {
|
|
setState(() {
|
|
final index = _todos.indexWhere((todo) => todo.id == id);
|
|
if (index != -1) {
|
|
_todos[index] = _todos[index].copyWith(
|
|
isCompleted: !_todos[index].isCompleted,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_titleController.dispose();
|
|
_descriptionController.dispose();
|
|
super.dispose();
|
|
}
|
|
} |