feat:增加代办事项状态管理
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import 'package:flisp_app/store/todo_dialog_store.dart';
|
||||
import 'package:flisp_app/widgets/todo_widget.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.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;
|
||||
@@ -16,28 +18,21 @@ class TodoPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class TodoPageState extends State<TodoPage> {
|
||||
// 添加一个公共方法供外部调用
|
||||
void showAddTodoDialog() {
|
||||
_showAddTodoDialog();
|
||||
}
|
||||
|
||||
// 待办事项列表
|
||||
final List<TodoItem> _todos = [
|
||||
// ... 原有的待办事项数据保持不变
|
||||
];
|
||||
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);
|
||||
}
|
||||
|
||||
// 添加一个公共方法供外部调用
|
||||
void showAddTodoDialog() {
|
||||
_showAddTodoDialog();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return buildBody(
|
||||
@@ -79,144 +74,64 @@ class TodoPageState extends State<TodoPage> {
|
||||
|
||||
// 显示添加待办事项对话框
|
||||
void _showAddTodoDialog() {
|
||||
_resetForm();
|
||||
final store = Provider.of<TodoDialogStore>(context, listen: false);
|
||||
store.reset();
|
||||
|
||||
showAwesomeDialog(
|
||||
context: context,
|
||||
body: _buildTodoDialogContent(isEditing: false),
|
||||
onOk: () => _saveTodo(false, null),
|
||||
onCancel: () => _resetForm(),
|
||||
body: TodoDialog(isEditing: false),
|
||||
onOk: () {
|
||||
if (store.validate()) {
|
||||
_saveTodo(false, null);
|
||||
}
|
||||
},
|
||||
onCancel: () {
|
||||
store.reset();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// 显示编辑待办事项对话框
|
||||
void _showEditTodoDialog(TodoItem todo) {
|
||||
_titleController.text = todo.title;
|
||||
_descriptionController.text = todo.description ?? '';
|
||||
_selectedDueDate = todo.dueDate;
|
||||
_selectedPriority = todo.priority;
|
||||
_selectedCategory = todo.category;
|
||||
final store = Provider.of<TodoDialogStore>(context, listen: false);
|
||||
store.initEditData(todo);
|
||||
|
||||
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();
|
||||
body: TodoDialog(isEditing: true, initialTodo: todo),
|
||||
onOk: () {
|
||||
if (store.validate()) {
|
||||
_saveTodo(true, todo);
|
||||
}
|
||||
},
|
||||
onSelectDueDate: () => _selectDueDateInDialog(isEditing),
|
||||
onCancel: () {
|
||||
store.reset();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// 在对话框中选择日期
|
||||
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 store = Provider.of<TodoDialogStore>(context, listen: false);
|
||||
|
||||
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,
|
||||
final newTodo = store.createTodoItem(
|
||||
id: isEditing ? todo!.id : null,
|
||||
isCompleted: isEditing ? todo!.isCompleted : false,
|
||||
createdAt: isEditing ? todo!.createdAt : null,
|
||||
);
|
||||
|
||||
setState(() {
|
||||
if (isEditing) {
|
||||
final index = _todos.indexWhere((t) => t.id == todo!.id);
|
||||
if (index != -1) {
|
||||
_todos[index] = newTodo.copyWith(
|
||||
isCompleted: _todos[index].isCompleted,
|
||||
);
|
||||
_todos[index] = newTodo;
|
||||
}
|
||||
} else {
|
||||
_todos.insert(0, newTodo);
|
||||
}
|
||||
});
|
||||
|
||||
_resetForm();
|
||||
|
||||
// 显示成功提示
|
||||
store.reset();
|
||||
_showSuccessDialog(isEditing ? '更新成功' : '添加成功');
|
||||
}
|
||||
|
||||
@@ -245,11 +160,4 @@ class TodoPageState extends State<TodoPage> {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleController.dispose();
|
||||
_descriptionController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user