feat:更新代办事项存储功能
This commit is contained in:
@@ -18,10 +18,9 @@ void main() async{
|
||||
Hive.registerAdapter(TodoAdapter());
|
||||
|
||||
// 打开Box
|
||||
await Hive.openBox<Todo>('todos');
|
||||
final todosBox = await Hive.openBox<Todo>('todos');
|
||||
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
print('📁 Hive数据库路径: ${appDir.path}');
|
||||
todosBox.clear();
|
||||
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ class Todo extends HiveObject {
|
||||
|
||||
static Todo getEmpty() {
|
||||
return Todo(
|
||||
id: 0,
|
||||
id: DateTime.now().millisecondsSinceEpoch,
|
||||
title: '',
|
||||
content: '',
|
||||
isCompleted: false,
|
||||
|
||||
@@ -17,7 +17,7 @@ class TodoAdapter extends TypeAdapter<Todo> {
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return Todo(
|
||||
id: fields[0] as int?,
|
||||
id: fields[0] as int,
|
||||
title: fields[1] as String,
|
||||
content: fields[2] as String,
|
||||
isCompleted: fields[3] as bool,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -97,9 +66,9 @@ class TodoPageState extends State<TodoPage> {
|
||||
? buildEmptyState(_currentTab)
|
||||
: buildTodoList(
|
||||
todos: _activeTodos,
|
||||
onToggleTodo: (todoId) {
|
||||
onToggleTodo: (todo) {
|
||||
setState(() {
|
||||
_toggleTodo(todoId);
|
||||
_toggleTodo(todo);
|
||||
});
|
||||
},
|
||||
onEditTodo: (todo) {
|
||||
@@ -109,14 +78,9 @@ class TodoPageState extends State<TodoPage> {
|
||||
}
|
||||
|
||||
// 切换待办事项完成状态
|
||||
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();
|
||||
if (isSuccess) {
|
||||
showSuccessDialog(context, isEditing ? '更新成功' : '添加成功');
|
||||
} else {
|
||||
showErrorDialog(context, isEditing ? '更新失败' : '添加失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flisp_app/models/todo.dart';
|
||||
|
||||
class TodoProvider with ChangeNotifier {
|
||||
Todo _formItem = Todo.getEmpty();
|
||||
late Todo _formItem;
|
||||
|
||||
Todo get formItem => _formItem;
|
||||
|
||||
@@ -12,10 +12,7 @@ class TodoProvider with ChangeNotifier {
|
||||
}
|
||||
|
||||
void initForm(Todo todo) {
|
||||
_formItem.title = todo.title;
|
||||
_formItem.content = todo.content ?? '';
|
||||
_formItem.dueDate = todo.dueDate;
|
||||
_formItem.priority = todo.priority;
|
||||
_formItem = todo;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,19 +6,39 @@ class TodoService {
|
||||
|
||||
Box<Todo> get box => Hive.box<Todo>(boxName);
|
||||
|
||||
Future<void> addTodo(Todo todo) async {
|
||||
Future<bool> addTodo(Todo todo) async {
|
||||
try {
|
||||
await box.add(todo);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
List<Todo> getAllTodos() {
|
||||
return box.values.toList();
|
||||
}
|
||||
|
||||
Future<void> updateTodo(Todo todo) async {
|
||||
Future<bool> updateTodo(Todo todo) async {
|
||||
try {
|
||||
await todo.save();
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteTodo(Todo todo) async {
|
||||
Future<bool> updateTodoCompletion(Todo todo, bool isCompleted) async {
|
||||
todo.isCompleted = isCompleted;
|
||||
return await updateTodo(todo);
|
||||
}
|
||||
|
||||
Future<bool> deleteTodo(Todo todo) async {
|
||||
try {
|
||||
await todo.delete();
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,3 +52,17 @@ void showSuccessDialog(BuildContext context, String message) {
|
||||
autoHide: Duration(seconds: 2),
|
||||
).show();
|
||||
}
|
||||
|
||||
// 显示失败提示
|
||||
void showErrorDialog(BuildContext context, String message) {
|
||||
AwesomeDialog(
|
||||
context: context,
|
||||
dialogType: DialogType.error,
|
||||
animType: AnimType.scale,
|
||||
title: message,
|
||||
btnOkText: "好的",
|
||||
btnOkColor: Colors.green,
|
||||
btnOkOnPress: () {},
|
||||
autoHide: Duration(seconds: 2),
|
||||
).show();
|
||||
}
|
||||
@@ -25,14 +25,6 @@ class _TodoFormState extends State<TodoForm> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
// 如果是编辑模式,初始化数据
|
||||
if (widget.isEditing && widget.initialTodo != null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final store = Provider.of<TodoProvider>(context, listen: false);
|
||||
store.initForm(widget.initialTodo!);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -201,7 +193,8 @@ class _TodoFormState extends State<TodoForm> {
|
||||
: null,
|
||||
),
|
||||
validator: (value) {
|
||||
if (value != null && value.isBefore(DateTime.now())) {
|
||||
if (value != null &&
|
||||
value.isBefore(DateTime.now().subtract(Duration(days: 1)))) {
|
||||
return '不能选择过去的日期';
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -71,7 +71,7 @@ Widget buildTabs({
|
||||
|
||||
Widget buildTodoList({
|
||||
required List<Todo> todos,
|
||||
required ValueChanged<String> onToggleTodo,
|
||||
required ValueChanged<Todo> onToggleTodo,
|
||||
required ValueChanged<Todo> onEditTodo,
|
||||
}) {
|
||||
return ListView.separated(
|
||||
@@ -90,7 +90,7 @@ Widget buildTodoList({
|
||||
|
||||
Widget _buildTodoItem({
|
||||
required Todo todo,
|
||||
required ValueChanged<String> onToggle,
|
||||
required ValueChanged<Todo> onToggle,
|
||||
required ValueChanged<Todo> onEdit,
|
||||
}) {
|
||||
return buildCard(
|
||||
@@ -100,7 +100,7 @@ Widget _buildTodoItem({
|
||||
width: 24,
|
||||
child: Checkbox(
|
||||
value: todo.isCompleted,
|
||||
onChanged: (value) => onToggle(todo.id.toString()),
|
||||
onChanged: (value) => onToggle(todo),
|
||||
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user