209 lines
5.5 KiB
Dart
209 lines
5.5 KiB
Dart
import 'package:flisp_app/provider/todo_provider.dart';
|
|
import 'package:flisp_app/service/todo_service.dart';
|
|
import 'package:flisp_app/utils/notify_utils.dart';
|
|
import 'package:flisp_app/widgets/todo_widget.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_common/widget/dialog_widget.dart';
|
|
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
|
import 'package:provider/provider.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_form.dart';
|
|
|
|
class TodoPage extends StatefulWidget {
|
|
final VoidCallback? onAddTodoPressed;
|
|
|
|
const TodoPage({super.key, this.onAddTodoPressed});
|
|
|
|
@override
|
|
State<TodoPage> createState() => TodoPageState();
|
|
}
|
|
|
|
class TodoPageState extends State<TodoPage> {
|
|
final TodoService todoService = TodoService();
|
|
final NotifyService notifyService = NotifyService();
|
|
|
|
late List<Todo> _todos;
|
|
TodoTab _currentTab = TodoTab.active;
|
|
late TodoSortMode _selectedMode = TodoSortMode.time;
|
|
|
|
// 获取过滤后的待办事项
|
|
List<Todo> get _activeTodos => getActiveTodos(_currentTab, _todos);
|
|
|
|
void showAddDialog() {
|
|
_showDialog(false, null);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_refreshTodos();
|
|
}
|
|
|
|
void _refreshTodos() {
|
|
setState(() {
|
|
_todos = todoService.getAllTodos(_selectedMode);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
children: [
|
|
_buildTodoTabs(),
|
|
_buildTodoSegment(),
|
|
SizedBox(height: 6),
|
|
Expanded(child: _buildActiveTodoList(context)),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildTodoTabs() {
|
|
return buildTabs(
|
|
context: context,
|
|
currentTab: _currentTab,
|
|
onTabChanged: (value) {
|
|
setState(() {
|
|
_currentTab = value;
|
|
});
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _buildTodoSegment() {
|
|
final colors = Theme.of(context).colorScheme;
|
|
|
|
return SegmentedButton<TodoSortMode>(
|
|
showSelectedIcon: false,
|
|
emptySelectionAllowed: false,
|
|
multiSelectionEnabled: false,
|
|
segments: [
|
|
ButtonSegment<TodoSortMode>(
|
|
value: TodoSortMode.time,
|
|
icon: Icon(Icons.access_time_filled, size: 16),
|
|
label: const Text('时间', style: TextStyle(fontSize: 12)),
|
|
),
|
|
ButtonSegment<TodoSortMode>(
|
|
value: TodoSortMode.priority,
|
|
icon: Icon(Icons.flag_outlined, size: 16),
|
|
label: const Text('优先级', style: TextStyle(fontSize: 12)),
|
|
),
|
|
],
|
|
selected: {_selectedMode},
|
|
onSelectionChanged: (Set<TodoSortMode> newSelection) {
|
|
setState(() {
|
|
_selectedMode = newSelection.first;
|
|
});
|
|
_refreshTodos();
|
|
},
|
|
style: buildSegmentStyle(colors),
|
|
);
|
|
}
|
|
|
|
Widget _buildActiveTodoList(BuildContext context) {
|
|
return _activeTodos.isEmpty
|
|
? buildEmptyState(context, _currentTab)
|
|
: buildTodoList(
|
|
context: context,
|
|
todos: _activeTodos,
|
|
onToggleTodo: (todo) {
|
|
setState(() {
|
|
_toggleTodo(todo);
|
|
});
|
|
},
|
|
onEditTodo: (todo) {
|
|
_showDialog(true, todo);
|
|
},
|
|
onDelete: (todo) {
|
|
_deleteTodo(todo);
|
|
},
|
|
);
|
|
}
|
|
|
|
// 切换待办事项完成状态
|
|
void _toggleTodo(Todo todo) async {
|
|
final bool? result = await showConfirmDialog(context, '确定已完成该待办吗?');
|
|
if (result == true) {
|
|
await notifyService.cancelNotification(todo.id);
|
|
|
|
setState(() {
|
|
todo.isCompleted = !todo.isCompleted;
|
|
todo.scheduledTime = null;
|
|
todoService.updateTodo(todo);
|
|
});
|
|
}
|
|
}
|
|
|
|
// 显示对话框
|
|
void _showDialog(bool isEditing, Todo? todo) {
|
|
final todoProvider = Provider.of<TodoProvider>(context, listen: false);
|
|
|
|
if (isEditing) {
|
|
todoProvider.initForm(todo!);
|
|
} else {
|
|
todoProvider.resetForm();
|
|
}
|
|
|
|
final formKey = GlobalKey<FormBuilderState>();
|
|
|
|
showAwesomeDialog(
|
|
context: context,
|
|
body: TodoForm(formKey: formKey, isEditing: isEditing, initialTodo: todo),
|
|
onOk: () {
|
|
if (formKey.currentState!.saveAndValidate()) {
|
|
Navigator.of(context).pop();
|
|
_saveTodo(isEditing, todoProvider.formItem);
|
|
}
|
|
},
|
|
onCancel: () {
|
|
todoProvider.resetForm();
|
|
},
|
|
);
|
|
}
|
|
|
|
// 保存待办事项
|
|
void _saveTodo(bool isEditing, Todo todo) async {
|
|
late bool isSuccess;
|
|
if (isEditing) {
|
|
// 先删除
|
|
await notifyService.cancelNotification(todo.id);
|
|
await todoService.deleteTodo(todo);
|
|
// 在重新新建
|
|
todo.id = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
|
}
|
|
|
|
isSuccess = await todoService.addTodo(todo);
|
|
|
|
if (todo.scheduledTime != null) {
|
|
await notifyService.scheduleNotification(
|
|
id: todo.id,
|
|
title: '待办提醒',
|
|
body: todo.title,
|
|
scheduledTime: todo.scheduledTime!,
|
|
);
|
|
}
|
|
|
|
_refreshTodos();
|
|
|
|
if (isSuccess) {
|
|
showSuccessTip(context, isEditing ? '更新成功' : '添加成功');
|
|
} else {
|
|
showErrorTip(context, isEditing ? '更新失败' : '添加失败');
|
|
}
|
|
}
|
|
|
|
void _deleteTodo(Todo todo) async {
|
|
await notifyService.cancelNotification(todo.id);
|
|
bool isSuccess = await todoService.deleteTodo(todo);
|
|
|
|
_refreshTodos();
|
|
|
|
if (isSuccess) {
|
|
showSuccessTip(context, '删除成功');
|
|
} else {
|
|
showErrorTip(context, '删除失败');
|
|
}
|
|
}
|
|
}
|