feat:更新代办事项存储功能

This commit is contained in:
2025-11-07 21:24:09 +08:00
parent 00e65bc5be
commit 27f0638c01
9 changed files with 80 additions and 92 deletions

View File

@@ -22,17 +22,12 @@ class TodoPage extends StatefulWidget {
class TodoPageState extends State<TodoPage> {
final TodoService todoService = TodoService();
// 待办事项列表
late List<Todo> _todos = [];
TodoTab _currentTab = TodoTab.all;
late List<Todo> _todos;
TodoTab _currentTab = TodoTab.active;
// 获取过滤后的待办事项
List<Todo> get _activeTodos {
return getActiveTodos(_currentTab, _todos);
}
List<Todo> get _activeTodos => getActiveTodos(_currentTab, _todos);
// 添加一个公共方法供外部调用
void showAddTodoDialog() {
_showTodoDialog(false, null);
}
@@ -40,33 +35,7 @@ class TodoPageState extends State<TodoPage> {
@override
void initState() {
super.initState();
// _testAddTodo();
_testQueryTodos();
}
void _testQueryTodos() {
try {
_todos = todoService.getAllTodos();
} catch (e) {
print("失败");
print(e);
}
}
Future<void> _testAddTodo() async {
try {
// final newTodo = await todoService.addTodo(
// title: '测试待办事项',
// content: '这是一个测试内容',
// priority: TodoPriority.high,
// dueDate: DateTime.now().add(Duration(days: 3)),
// );
print("成功");
} catch (e) {
print("失败");
print(e);
}
_todos = todoService.getAllTodos();
}
@override
@@ -96,27 +65,22 @@ class TodoPageState extends State<TodoPage> {
return _activeTodos.isEmpty
? buildEmptyState(_currentTab)
: buildTodoList(
todos: _activeTodos,
onToggleTodo: (todoId) {
setState(() {
_toggleTodo(todoId);
});
},
onEditTodo: (todo) {
_showTodoDialog(true, todo);
},
);
todos: _activeTodos,
onToggleTodo: (todo) {
setState(() {
_toggleTodo(todo);
});
},
onEditTodo: (todo) {
_showTodoDialog(true, todo);
},
);
}
// 切换待办事项完成状态
void _toggleTodo(String id) {
void _toggleTodo(Todo todo) {
setState(() {
final index = _todos.indexWhere((todo) => todo.id.toString() == id);
if (index != -1) {
_todos[index] = _todos[index].copyWith(
isCompleted: !_todos[index].isCompleted,
);
}
todoService.updateTodoCompletion(todo, !todo.isCompleted);
});
}
@@ -148,21 +112,22 @@ class TodoPageState extends State<TodoPage> {
}
// 保存待办事项
void _saveTodo(bool isEditing, Todo todo) {
final store = Provider.of<TodoProvider>(context, listen: false);
void _saveTodo(bool isEditing, Todo todo) async {
late bool isSuccess ;
if (isEditing) {
isSuccess = await todoService.updateTodo(todo);
} else {
isSuccess = await todoService.addTodo(todo);
}
setState(() {
if (isEditing) {
final index = _todos.indexWhere((t) => t.id == todo.id);
if (index != -1) {
_todos[index] = todo;
}
} else {
_todos.insert(0, todo);
}
_todos = todoService.getAllTodos();
});
store.resetForm();
showSuccessDialog(context, isEditing ? '更新成功' : '添加成功');
if (isSuccess) {
showSuccessDialog(context, isEditing ? '更新成功' : '添加成功');
} else {
showErrorDialog(context, isEditing ? '更新失败' : '添加失败');
}
}
}