feat:更新代办事项存储功能
This commit is contained in:
@@ -18,10 +18,9 @@ void main() async{
|
|||||||
Hive.registerAdapter(TodoAdapter());
|
Hive.registerAdapter(TodoAdapter());
|
||||||
|
|
||||||
// 打开Box
|
// 打开Box
|
||||||
await Hive.openBox<Todo>('todos');
|
final todosBox = await Hive.openBox<Todo>('todos');
|
||||||
|
|
||||||
final appDir = await getApplicationDocumentsDirectory();
|
todosBox.clear();
|
||||||
print('📁 Hive数据库路径: ${appDir.path}');
|
|
||||||
|
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ class Todo extends HiveObject {
|
|||||||
|
|
||||||
static Todo getEmpty() {
|
static Todo getEmpty() {
|
||||||
return Todo(
|
return Todo(
|
||||||
id: 0,
|
id: DateTime.now().millisecondsSinceEpoch,
|
||||||
title: '',
|
title: '',
|
||||||
content: '',
|
content: '',
|
||||||
isCompleted: false,
|
isCompleted: false,
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class TodoAdapter extends TypeAdapter<Todo> {
|
|||||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||||
};
|
};
|
||||||
return Todo(
|
return Todo(
|
||||||
id: fields[0] as int?,
|
id: fields[0] as int,
|
||||||
title: fields[1] as String,
|
title: fields[1] as String,
|
||||||
content: fields[2] as String,
|
content: fields[2] as String,
|
||||||
isCompleted: fields[3] as bool,
|
isCompleted: fields[3] as bool,
|
||||||
|
|||||||
@@ -22,17 +22,12 @@ class TodoPage extends StatefulWidget {
|
|||||||
class TodoPageState extends State<TodoPage> {
|
class TodoPageState extends State<TodoPage> {
|
||||||
final TodoService todoService = TodoService();
|
final TodoService todoService = TodoService();
|
||||||
|
|
||||||
// 待办事项列表
|
late List<Todo> _todos;
|
||||||
late List<Todo> _todos = [];
|
TodoTab _currentTab = TodoTab.active;
|
||||||
|
|
||||||
TodoTab _currentTab = TodoTab.all;
|
|
||||||
|
|
||||||
// 获取过滤后的待办事项
|
// 获取过滤后的待办事项
|
||||||
List<Todo> get _activeTodos {
|
List<Todo> get _activeTodos => getActiveTodos(_currentTab, _todos);
|
||||||
return getActiveTodos(_currentTab, _todos);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加一个公共方法供外部调用
|
|
||||||
void showAddTodoDialog() {
|
void showAddTodoDialog() {
|
||||||
_showTodoDialog(false, null);
|
_showTodoDialog(false, null);
|
||||||
}
|
}
|
||||||
@@ -40,33 +35,7 @@ class TodoPageState extends State<TodoPage> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
_todos = todoService.getAllTodos();
|
||||||
// _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
|
@override
|
||||||
@@ -96,27 +65,22 @@ class TodoPageState extends State<TodoPage> {
|
|||||||
return _activeTodos.isEmpty
|
return _activeTodos.isEmpty
|
||||||
? buildEmptyState(_currentTab)
|
? buildEmptyState(_currentTab)
|
||||||
: buildTodoList(
|
: buildTodoList(
|
||||||
todos: _activeTodos,
|
todos: _activeTodos,
|
||||||
onToggleTodo: (todoId) {
|
onToggleTodo: (todo) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_toggleTodo(todoId);
|
_toggleTodo(todo);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onEditTodo: (todo) {
|
onEditTodo: (todo) {
|
||||||
_showTodoDialog(true, todo);
|
_showTodoDialog(true, todo);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 切换待办事项完成状态
|
// 切换待办事项完成状态
|
||||||
void _toggleTodo(String id) {
|
void _toggleTodo(Todo todo) {
|
||||||
setState(() {
|
setState(() {
|
||||||
final index = _todos.indexWhere((todo) => todo.id.toString() == id);
|
todoService.updateTodoCompletion(todo, !todo.isCompleted);
|
||||||
if (index != -1) {
|
|
||||||
_todos[index] = _todos[index].copyWith(
|
|
||||||
isCompleted: !_todos[index].isCompleted,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,21 +112,22 @@ class TodoPageState extends State<TodoPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 保存待办事项
|
// 保存待办事项
|
||||||
void _saveTodo(bool isEditing, Todo todo) {
|
void _saveTodo(bool isEditing, Todo todo) async {
|
||||||
final store = Provider.of<TodoProvider>(context, listen: false);
|
late bool isSuccess ;
|
||||||
|
if (isEditing) {
|
||||||
|
isSuccess = await todoService.updateTodo(todo);
|
||||||
|
} else {
|
||||||
|
isSuccess = await todoService.addTodo(todo);
|
||||||
|
}
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
if (isEditing) {
|
_todos = todoService.getAllTodos();
|
||||||
final index = _todos.indexWhere((t) => t.id == todo.id);
|
|
||||||
if (index != -1) {
|
|
||||||
_todos[index] = todo;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_todos.insert(0, todo);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
store.resetForm();
|
if (isSuccess) {
|
||||||
showSuccessDialog(context, isEditing ? '更新成功' : '添加成功');
|
showSuccessDialog(context, isEditing ? '更新成功' : '添加成功');
|
||||||
|
} else {
|
||||||
|
showErrorDialog(context, isEditing ? '更新失败' : '添加失败');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flisp_app/models/todo.dart';
|
import 'package:flisp_app/models/todo.dart';
|
||||||
|
|
||||||
class TodoProvider with ChangeNotifier {
|
class TodoProvider with ChangeNotifier {
|
||||||
Todo _formItem = Todo.getEmpty();
|
late Todo _formItem;
|
||||||
|
|
||||||
Todo get formItem => _formItem;
|
Todo get formItem => _formItem;
|
||||||
|
|
||||||
@@ -12,10 +12,7 @@ class TodoProvider with ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void initForm(Todo todo) {
|
void initForm(Todo todo) {
|
||||||
_formItem.title = todo.title;
|
_formItem = todo;
|
||||||
_formItem.content = todo.content ?? '';
|
|
||||||
_formItem.dueDate = todo.dueDate;
|
|
||||||
_formItem.priority = todo.priority;
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,19 +6,39 @@ class TodoService {
|
|||||||
|
|
||||||
Box<Todo> get box => Hive.box<Todo>(boxName);
|
Box<Todo> get box => Hive.box<Todo>(boxName);
|
||||||
|
|
||||||
Future<void> addTodo(Todo todo) async {
|
Future<bool> addTodo(Todo todo) async {
|
||||||
await box.add(todo);
|
try {
|
||||||
|
await box.add(todo);
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Todo> getAllTodos() {
|
List<Todo> getAllTodos() {
|
||||||
return box.values.toList();
|
return box.values.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> updateTodo(Todo todo) async {
|
Future<bool> updateTodo(Todo todo) async {
|
||||||
await todo.save();
|
try {
|
||||||
|
await todo.save();
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> deleteTodo(Todo todo) async {
|
Future<bool> updateTodoCompletion(Todo todo, bool isCompleted) async {
|
||||||
await todo.delete();
|
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),
|
autoHide: Duration(seconds: 2),
|
||||||
).show();
|
).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
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.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
|
@override
|
||||||
@@ -201,7 +193,8 @@ class _TodoFormState extends State<TodoForm> {
|
|||||||
: null,
|
: null,
|
||||||
),
|
),
|
||||||
validator: (value) {
|
validator: (value) {
|
||||||
if (value != null && value.isBefore(DateTime.now())) {
|
if (value != null &&
|
||||||
|
value.isBefore(DateTime.now().subtract(Duration(days: 1)))) {
|
||||||
return '不能选择过去的日期';
|
return '不能选择过去的日期';
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ Widget buildTabs({
|
|||||||
|
|
||||||
Widget buildTodoList({
|
Widget buildTodoList({
|
||||||
required List<Todo> todos,
|
required List<Todo> todos,
|
||||||
required ValueChanged<String> onToggleTodo,
|
required ValueChanged<Todo> onToggleTodo,
|
||||||
required ValueChanged<Todo> onEditTodo,
|
required ValueChanged<Todo> onEditTodo,
|
||||||
}) {
|
}) {
|
||||||
return ListView.separated(
|
return ListView.separated(
|
||||||
@@ -90,7 +90,7 @@ Widget buildTodoList({
|
|||||||
|
|
||||||
Widget _buildTodoItem({
|
Widget _buildTodoItem({
|
||||||
required Todo todo,
|
required Todo todo,
|
||||||
required ValueChanged<String> onToggle,
|
required ValueChanged<Todo> onToggle,
|
||||||
required ValueChanged<Todo> onEdit,
|
required ValueChanged<Todo> onEdit,
|
||||||
}) {
|
}) {
|
||||||
return buildCard(
|
return buildCard(
|
||||||
@@ -100,7 +100,7 @@ Widget _buildTodoItem({
|
|||||||
width: 24,
|
width: 24,
|
||||||
child: Checkbox(
|
child: Checkbox(
|
||||||
value: todo.isCompleted,
|
value: todo.isCompleted,
|
||||||
onChanged: (value) => onToggle(todo.id.toString()),
|
onChanged: (value) => onToggle(todo),
|
||||||
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user