feat:增加待办提醒功能
This commit is contained in:
@@ -1,13 +1,28 @@
|
||||
import 'package:fluent_ui/fluent_ui.dart';
|
||||
import 'package:hive_flutter/adapters.dart';
|
||||
import 'package:task_hub/models/task.dart';
|
||||
import 'package:task_hub/pages/home_page.dart';
|
||||
import 'package:task_hub/pages/schedule_page.dart';
|
||||
import 'package:task_hub/pages/settings_page.dart';
|
||||
import 'package:task_hub/pages/task_page.dart';
|
||||
import 'package:task_hub/utils/notice.dart';
|
||||
import 'package:timezone/data/latest.dart' as tz;
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowMinSize(const Size(800, 600));
|
||||
|
||||
tz.initializeTimeZones();
|
||||
|
||||
await Hive.initFlutter();
|
||||
Hive.registerAdapter(TaskAdapter());
|
||||
await Hive.openBox<Task>('taskBox');
|
||||
|
||||
await initNotify();
|
||||
|
||||
checkScheduleTime();
|
||||
|
||||
runApp(const TaskHubApp());
|
||||
}
|
||||
|
||||
@@ -21,7 +36,7 @@ class TaskHubApp extends StatelessWidget {
|
||||
themeMode: ThemeMode.system,
|
||||
theme: FluentThemeData(
|
||||
fontFamily: 'CustomFont',
|
||||
accentColor: Colors.purple
|
||||
accentColor: Colors.purple,
|
||||
),
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: const MainLayout(),
|
||||
|
||||
@@ -13,6 +13,6 @@ class Schedule {
|
||||
required this.endTime,
|
||||
this.scheduleTime,
|
||||
DateTime? createTime,
|
||||
}) : id = id ?? DateTime.timestamp().microsecond,
|
||||
createTime = DateTime.now();
|
||||
}
|
||||
}) : id = id ?? DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
||||
createTime = DateTime.now();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
// flutter packages pub run build_runner build
|
||||
// part 'todo.g.dart';
|
||||
part 'task.g.dart';
|
||||
|
||||
@HiveType(typeId: 0)
|
||||
class Task extends HiveObject {
|
||||
@@ -45,10 +45,11 @@ class Task extends HiveObject {
|
||||
this.isCompleted = false,
|
||||
Priority priority = Priority.medium,
|
||||
DateTime? createTime,
|
||||
}) : id = id ?? DateTime.timestamp().microsecond,
|
||||
DateTime? updateTime,
|
||||
}) : id = id ?? DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
||||
this.priorityIndex = priority.index,
|
||||
createTime = DateTime.now(),
|
||||
updateTime = DateTime.now();
|
||||
createTime = createTime ?? DateTime.now(),
|
||||
updateTime = updateTime ?? DateTime.now();
|
||||
}
|
||||
|
||||
enum Priority { low, medium, high }
|
||||
|
||||
65
lib/models/task.g.dart
Normal file
65
lib/models/task.g.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'task.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class TaskAdapter extends TypeAdapter<Task> {
|
||||
@override
|
||||
final int typeId = 0;
|
||||
|
||||
@override
|
||||
Task read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return Task(
|
||||
id: fields[0] as int?,
|
||||
title: fields[1] as String,
|
||||
desc: fields[2] as String?,
|
||||
dueDate: fields[3] as DateTime?,
|
||||
scheduleTime: fields[4] as DateTime?,
|
||||
isCompleted: fields[5] as bool,
|
||||
createTime: fields[7] as DateTime?,
|
||||
)
|
||||
..priorityIndex = fields[6] as int
|
||||
..updateTime = fields[8] as DateTime;
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, Task obj) {
|
||||
writer
|
||||
..writeByte(9)
|
||||
..writeByte(0)
|
||||
..write(obj.id)
|
||||
..writeByte(1)
|
||||
..write(obj.title)
|
||||
..writeByte(2)
|
||||
..write(obj.desc)
|
||||
..writeByte(3)
|
||||
..write(obj.dueDate)
|
||||
..writeByte(4)
|
||||
..write(obj.scheduleTime)
|
||||
..writeByte(5)
|
||||
..write(obj.isCompleted)
|
||||
..writeByte(6)
|
||||
..write(obj.priorityIndex)
|
||||
..writeByte(7)
|
||||
..write(obj.createTime)
|
||||
..writeByte(8)
|
||||
..write(obj.updateTime);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is TaskAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
// 初始化通知
|
||||
Future<void> _initNotify() async {
|
||||
final notify = FlutterLocalNotificationsPlugin();
|
||||
await notify.initialize(
|
||||
const InitializationSettings(
|
||||
windows: WindowsInitializationSettings(
|
||||
appName: 'TaskHub',
|
||||
appUserModelId: 'com.cxx.task',
|
||||
guid: 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
|
||||
iconPath: 'assets/icons/task.png'
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 发送通知
|
||||
Future<void> _showNotify(String title, String text) async {
|
||||
final notify = FlutterLocalNotificationsPlugin();
|
||||
final id = DateTime.now().millisecondsSinceEpoch.remainder(10000);
|
||||
|
||||
await notify.show(
|
||||
id,
|
||||
title,
|
||||
text,
|
||||
const NotificationDetails(
|
||||
windows: WindowsNotificationDetails(),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -2,7 +2,9 @@ import 'dart:ui';
|
||||
|
||||
import 'package:fluent_ui/fluent_ui.dart';
|
||||
import 'package:flutter/material.dart' show SegmentedButton, ButtonSegment;
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:task_hub/models/task.dart';
|
||||
import 'package:task_hub/utils/notice.dart';
|
||||
import 'package:task_hub/widgets/task_dialog.dart';
|
||||
import 'package:task_hub/widgets/task_item.dart';
|
||||
|
||||
@@ -15,10 +17,24 @@ class TaskPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _TaskPageState extends State<TaskPage> {
|
||||
final List<Task> _tasks = [];
|
||||
late Box<Task> _taskBox;
|
||||
List<Task> _tasks = [];
|
||||
String _filter = 'all';
|
||||
String _sortBy = 'created';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadTasks();
|
||||
}
|
||||
|
||||
void _loadTasks() {
|
||||
_taskBox = Hive.box<Task>('taskBox');
|
||||
setState(() {
|
||||
_tasks = _taskBox.values.toList();
|
||||
});
|
||||
}
|
||||
|
||||
List<Task> get _filteredTasks {
|
||||
List<Task> tasks = _tasks.where((task) {
|
||||
if (_filter == 'active') return !task.isCompleted;
|
||||
@@ -48,10 +64,12 @@ class _TaskPageState extends State<TaskPage> {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => TaskDialog(
|
||||
onSave: (task) {
|
||||
onSave: (task) async {
|
||||
await _taskBox.put(task.id, task);
|
||||
setState(() {
|
||||
_tasks.add(task);
|
||||
_tasks = _taskBox.values.toList();
|
||||
});
|
||||
checkScheduleTime();
|
||||
},
|
||||
),
|
||||
);
|
||||
@@ -62,22 +80,26 @@ class _TaskPageState extends State<TaskPage> {
|
||||
context: context,
|
||||
builder: (context) => TaskDialog(
|
||||
task: task,
|
||||
onSave: (editedTask) {
|
||||
onSave: (editedTask) async {
|
||||
editedTask.updateTime = DateTime.now();
|
||||
await _taskBox.put(editedTask.id, editedTask);
|
||||
setState(() {
|
||||
final index = _tasks.indexWhere((t) => t.id == task.id);
|
||||
if (index != -1) {
|
||||
_tasks[index] = editedTask;
|
||||
}
|
||||
_tasks = _taskBox.values.toList();
|
||||
});
|
||||
checkScheduleTime();
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _deleteTask(int taskId) {
|
||||
void _deleteTask(int taskId) async {
|
||||
final task = _tasks.firstWhere((t) => t.id == taskId);
|
||||
await task.delete();
|
||||
|
||||
setState(() {
|
||||
_tasks.removeWhere((task) => task.id == taskId);
|
||||
_tasks = _taskBox.values.toList();
|
||||
});
|
||||
checkScheduleTime();
|
||||
}
|
||||
|
||||
void _toggleTaskComplete(Task task) {
|
||||
@@ -91,7 +113,7 @@ class _TaskPageState extends State<TaskPage> {
|
||||
createTime: task.createTime,
|
||||
dueDate: task.dueDate,
|
||||
isCompleted: !task.isCompleted,
|
||||
priority: task.priority
|
||||
priority: task.priority,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
3
lib/utils/common.dart
Normal file
3
lib/utils/common.dart
Normal file
@@ -0,0 +1,3 @@
|
||||
bool isSameDate(DateTime a, DateTime b) {
|
||||
return a.year == b.year && a.month == b.month && a.day == b.day;
|
||||
}
|
||||
82
lib/utils/notice.dart
Normal file
82
lib/utils/notice.dart
Normal file
@@ -0,0 +1,82 @@
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:timezone/timezone.dart' as tz;
|
||||
|
||||
import '../models/task.dart';
|
||||
import 'common.dart';
|
||||
|
||||
// 初始化通知
|
||||
Future<void> initNotify() async {
|
||||
final notify = FlutterLocalNotificationsPlugin();
|
||||
await notify.initialize(
|
||||
const InitializationSettings(
|
||||
windows: WindowsInitializationSettings(
|
||||
appName: 'TaskHub',
|
||||
appUserModelId: 'com.cxx.task',
|
||||
guid: 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
|
||||
iconPath: 'assets/icons/app_icon.ico',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> showNotify(String title, String body) async {
|
||||
final notify = FlutterLocalNotificationsPlugin();
|
||||
final id = DateTime.now().millisecondsSinceEpoch.remainder(10000);
|
||||
|
||||
await notify.show(
|
||||
id,
|
||||
title,
|
||||
body,
|
||||
const NotificationDetails(windows: WindowsNotificationDetails()),
|
||||
);
|
||||
}
|
||||
|
||||
// 发送通知
|
||||
Future<void> scheduleNotification(
|
||||
int id,
|
||||
String title,
|
||||
String body,
|
||||
DateTime scheduleTime,
|
||||
) async {
|
||||
final notify = FlutterLocalNotificationsPlugin();
|
||||
|
||||
final local = tz.getLocation('Asia/Shanghai');
|
||||
final tzTime = tz.TZDateTime.from(scheduleTime, local);
|
||||
|
||||
await notify.zonedSchedule(
|
||||
id,
|
||||
title,
|
||||
body,
|
||||
tzTime,
|
||||
const NotificationDetails(windows: WindowsNotificationDetails()),
|
||||
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
|
||||
);
|
||||
}
|
||||
|
||||
// 取消通知
|
||||
Future<void> cancelAllSchedule() async {
|
||||
final notify = FlutterLocalNotificationsPlugin();
|
||||
|
||||
await notify.cancelAll();
|
||||
}
|
||||
|
||||
void checkScheduleTime() {
|
||||
final taskBox = Hive.box<Task>('taskBox');
|
||||
final now = DateTime.now();
|
||||
|
||||
cancelAllSchedule();
|
||||
for (var task in taskBox.values) {
|
||||
if (task.scheduleTime != null &&
|
||||
!task.isCompleted &&
|
||||
isSameDate(task.scheduleTime!, DateTime.now()) &&
|
||||
task.scheduleTime!.isAfter(now)) {
|
||||
scheduleNotification(
|
||||
task.id,
|
||||
task.title,
|
||||
task.desc ?? '',
|
||||
task.scheduleTime!,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,9 @@ Widget buildOmniDateButton({
|
||||
onPressed: () async {
|
||||
final DateTime? picked = await showOmniDateTimePicker(
|
||||
context: context,
|
||||
constraints: const BoxConstraints(
|
||||
maxWidth: 400,
|
||||
),
|
||||
initialDate: selectedDate ?? DateTime.now(),
|
||||
firstDate: DateTime(
|
||||
DateTime.now().year,
|
||||
|
||||
@@ -19,6 +19,7 @@ class _TaskDialogState extends State<TaskDialog> {
|
||||
late Priority _selectedPriority;
|
||||
DateTime? _selectedDueDate;
|
||||
DateTime? _selectedScheduleTime;
|
||||
DateTime? _createTime;
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
@@ -29,6 +30,7 @@ class _TaskDialogState extends State<TaskDialog> {
|
||||
_selectedPriority = widget.task?.priority ?? Priority.medium;
|
||||
_selectedDueDate = widget.task?.dueDate;
|
||||
_selectedScheduleTime = widget.task?.scheduleTime;
|
||||
_createTime = widget.task?.createTime;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -38,8 +40,22 @@ class _TaskDialogState extends State<TaskDialog> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void onConfirmClick() {
|
||||
void onConfirmClick() async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
if (_selectedScheduleTime != null &&
|
||||
_selectedScheduleTime!.isBefore(DateTime.now())) {
|
||||
await displayInfoBar(
|
||||
context,
|
||||
builder: (context, close) {
|
||||
return InfoBar(
|
||||
title: const Text('提醒时间不能早于当前时间'),
|
||||
severity: InfoBarSeverity.error,
|
||||
);
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final task = Task(
|
||||
id: widget.task?.id,
|
||||
title: _titleController.text,
|
||||
@@ -48,6 +64,7 @@ class _TaskDialogState extends State<TaskDialog> {
|
||||
scheduleTime: _selectedScheduleTime,
|
||||
priority: _selectedPriority,
|
||||
isCompleted: widget.task?.isCompleted ?? false,
|
||||
createTime: _createTime,
|
||||
);
|
||||
widget.onSave(task);
|
||||
Navigator.pop(context);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:fluent_ui/fluent_ui.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:task_hub/models/task.dart';
|
||||
|
||||
class TaskItem extends StatelessWidget {
|
||||
@@ -39,11 +40,14 @@ class TaskItem extends StatelessWidget {
|
||||
Expanded(child: _buildTitle(context)),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
if (task.desc != null && task.desc!.isNotEmpty) _buildDesc(),
|
||||
const SizedBox(height: 4),
|
||||
if (task.dueDate != null || task.scheduleTime != null)
|
||||
if (task.desc != null && task.desc!.isNotEmpty) ...[
|
||||
const SizedBox(height: 4),
|
||||
_buildDesc(),
|
||||
],
|
||||
if (task.dueDate != null || task.scheduleTime != null) ...[
|
||||
const SizedBox(height: 4),
|
||||
_buildDate(),
|
||||
]
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -92,7 +96,7 @@ class TaskItem extends StatelessWidget {
|
||||
Icon(FluentIcons.calendar, size: 12, color: Colors.grey),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'截止日期: ${_formatDate(task.dueDate!)}',
|
||||
'截止日期: ${DateFormat('yyyy-MM-dd').format(task.dueDate!)}',
|
||||
style: TextStyle(color: Colors.grey, fontSize: 12),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
@@ -101,7 +105,7 @@ class TaskItem extends StatelessWidget {
|
||||
Icon(FluentIcons.clock, size: 12, color: Colors.grey),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'提醒日期: ${_formatDate(task.scheduleTime!)}',
|
||||
'提醒日期: ${DateFormat('yyyy-MM-dd HH:mm:ss').format(task.scheduleTime!)}',
|
||||
style: TextStyle(color: Colors.grey, fontSize: 12),
|
||||
),
|
||||
],
|
||||
@@ -181,8 +185,4 @@ class TaskItem extends StatelessWidget {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user