fix:修复重启设备后没有通知提醒的问题
This commit is contained in:
55
lib/layout/home.dart
Normal file
55
lib/layout/home.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'package:flisp_app/layout/app_body.dart';
|
||||
import 'package:flisp_app/layout/app_drawer.dart';
|
||||
import 'package:flisp_app/layout/app_floating_button.dart';
|
||||
import 'package:flisp_app/layout/app_navbar.dart';
|
||||
import 'package:flisp_app/provider/app_provider.dart';
|
||||
import 'package:flisp_app/utils/notify_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class Home extends StatefulWidget {
|
||||
const Home({super.key});
|
||||
|
||||
@override
|
||||
State<Home> createState() => _HomeState();
|
||||
}
|
||||
|
||||
class _HomeState extends State<Home> {
|
||||
final GlobalKey<AppBodyState> _appBodyKey = GlobalKey();
|
||||
final NotifyService notifyService = NotifyService();
|
||||
|
||||
void _onPressedFloatingButton(int index) {
|
||||
_appBodyKey.currentState?.showPageAddDialog(index);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final provider = context.watch<AppProvider>();
|
||||
|
||||
return Scaffold(
|
||||
drawer: const AppDrawer(),
|
||||
appBar: AppBar(
|
||||
title: Text(provider.currentAppBarTitle),
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Theme.of(context).colorScheme.onPrimary,
|
||||
elevation: 0,
|
||||
),
|
||||
body: Padding(
|
||||
padding: EdgeInsets.all(10),
|
||||
child: AppBody(key: _appBodyKey),
|
||||
),
|
||||
bottomNavigationBar: AppNavbar(),
|
||||
floatingActionButton:
|
||||
provider.currentTab == 3
|
||||
? null
|
||||
: AppFloatingButton(
|
||||
onPressed: () => _onPressedFloatingButton(provider.currentTab),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
import 'package:flisp_app/layout/app_body.dart';
|
||||
import 'package:flisp_app/layout/app_drawer.dart';
|
||||
import 'package:flisp_app/layout/app_floating_button.dart';
|
||||
import 'package:flisp_app/layout/app_navbar.dart';
|
||||
import 'package:flisp_app/models/calendar.dart';
|
||||
import 'package:flisp_app/models/todo.dart';
|
||||
import 'package:flisp_app/provider/app_provider.dart';
|
||||
import 'package:flisp_app/service/calendar_service.dart';
|
||||
import 'package:flisp_app/service/todo_service.dart';
|
||||
import 'package:flisp_app/utils/notify_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_common/utils/date_utils.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class MainScreen extends StatefulWidget {
|
||||
const MainScreen({super.key});
|
||||
|
||||
@override
|
||||
State<MainScreen> createState() => _MainScreenState();
|
||||
}
|
||||
|
||||
class _MainScreenState extends State<MainScreen> {
|
||||
final GlobalKey<AppBodyState> _appBodyKey = GlobalKey();
|
||||
final NotifyService notifyService = NotifyService();
|
||||
|
||||
void _onPressedFloatingButton(int index) {
|
||||
_appBodyKey.currentState?.showPageAddDialog(index);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
refreshScheduled();
|
||||
}
|
||||
|
||||
/// 重新刷新提醒 防止每次重启设备后提醒丢失的问题
|
||||
void refreshScheduled() async {
|
||||
await notifyService.cancelAllNotifications();
|
||||
|
||||
TodoService todoService = TodoService();
|
||||
final List<Todo> todoResult = todoService.getAllTodos(
|
||||
TodoSortMode.priority,
|
||||
);
|
||||
final List<Todo> todayTodos =
|
||||
todoResult
|
||||
.where(
|
||||
(todo) => !todo.isCompleted && isAfterToday(todo.scheduledTime),
|
||||
)
|
||||
.toList();
|
||||
|
||||
for (Todo todo in todayTodos) {
|
||||
await notifyService.scheduleNotification(
|
||||
id: todo.id,
|
||||
title: '待办提醒',
|
||||
body: todo.title,
|
||||
scheduledTime: todo.scheduledTime!,
|
||||
);
|
||||
}
|
||||
|
||||
CalendarService calendarService = CalendarService();
|
||||
final List<Calendar> calendarResult = calendarService.getAllCalendars();
|
||||
final List<Calendar> todayCalendars =
|
||||
calendarResult
|
||||
.where((item) => isAfterToday(item.scheduledTime))
|
||||
.toList();
|
||||
|
||||
for (Calendar calendar in todayCalendars) {
|
||||
await notifyService.scheduleNotification(
|
||||
id: calendar.id,
|
||||
title: '日程提醒',
|
||||
body: calendar.title,
|
||||
scheduledTime: calendar.scheduledTime!,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final provider = context.watch<AppProvider>();
|
||||
|
||||
return Scaffold(
|
||||
drawer: const AppDrawer(),
|
||||
appBar: AppBar(
|
||||
title: Text(provider.currentAppBarTitle),
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Theme.of(context).colorScheme.onPrimary,
|
||||
elevation: 0,
|
||||
),
|
||||
body: Padding(
|
||||
padding: EdgeInsets.all(10),
|
||||
child: AppBody(key: _appBodyKey),
|
||||
),
|
||||
bottomNavigationBar: AppNavbar(),
|
||||
floatingActionButton:
|
||||
provider.currentTab == 3
|
||||
? null
|
||||
: AppFloatingButton(
|
||||
onPressed: () => _onPressedFloatingButton(provider.currentTab),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import 'package:hive_flutter/adapters.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:syncfusion_localizations/syncfusion_localizations.dart';
|
||||
|
||||
import 'layout/main_screen.dart';
|
||||
import 'layout/home.dart';
|
||||
import 'models/todo.dart';
|
||||
import 'provider/todo_provider.dart';
|
||||
|
||||
@@ -81,7 +81,7 @@ class MyApp extends StatelessWidget {
|
||||
supportedLocales: [const Locale('zh'), const Locale('zh', 'CN')],
|
||||
locale: Locale('zh', 'CN'),
|
||||
theme: themeProvider.currentThemeData,
|
||||
home: const MainScreen(),
|
||||
home: const Home(),
|
||||
debugShowCheckedModeBanner: false,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
import 'package:flisp_app/models/calendar.dart';
|
||||
import 'package:flisp_app/models/todo.dart';
|
||||
import 'package:flisp_app/service/calendar_service.dart';
|
||||
import 'package:flisp_app/service/todo_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_common/utils/date_utils.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:timezone/data/latest_all.dart' as tz;
|
||||
import 'package:timezone/timezone.dart' as tz;
|
||||
@@ -45,6 +51,51 @@ class NotifyService {
|
||||
);
|
||||
|
||||
await _notifications.initialize(settings);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_refreshScheduled();
|
||||
});
|
||||
}
|
||||
|
||||
/// 重新刷新提醒 防止每次重启设备后提醒丢失的问题
|
||||
void _refreshScheduled() async {
|
||||
await cancelAllNotifications();
|
||||
|
||||
TodoService todoService = TodoService();
|
||||
final List<Todo> todoResult = todoService.getAllTodos(
|
||||
TodoSortMode.priority,
|
||||
);
|
||||
final List<Todo> todayTodos =
|
||||
todoResult
|
||||
.where(
|
||||
(todo) => !todo.isCompleted && isAfterToday(todo.scheduledTime),
|
||||
)
|
||||
.toList();
|
||||
|
||||
for (Todo todo in todayTodos) {
|
||||
await scheduleNotification(
|
||||
id: todo.id,
|
||||
title: '待办提醒',
|
||||
body: todo.title,
|
||||
scheduledTime: todo.scheduledTime!,
|
||||
);
|
||||
}
|
||||
|
||||
CalendarService calendarService = CalendarService();
|
||||
final List<Calendar> calendarResult = calendarService.getAllCalendars();
|
||||
final List<Calendar> todayCalendars =
|
||||
calendarResult
|
||||
.where((item) => isAfterToday(item.scheduledTime))
|
||||
.toList();
|
||||
|
||||
for (Calendar calendar in todayCalendars) {
|
||||
await scheduleNotification(
|
||||
id: calendar.id,
|
||||
title: '日程提醒',
|
||||
body: calendar.title,
|
||||
scheduledTime: calendar.scheduledTime!,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建Android通知详情
|
||||
|
||||
Reference in New Issue
Block a user