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:provider/provider.dart';
|
||||||
import 'package:syncfusion_localizations/syncfusion_localizations.dart';
|
import 'package:syncfusion_localizations/syncfusion_localizations.dart';
|
||||||
|
|
||||||
import 'layout/main_screen.dart';
|
import 'layout/home.dart';
|
||||||
import 'models/todo.dart';
|
import 'models/todo.dart';
|
||||||
import 'provider/todo_provider.dart';
|
import 'provider/todo_provider.dart';
|
||||||
|
|
||||||
@@ -81,7 +81,7 @@ class MyApp extends StatelessWidget {
|
|||||||
supportedLocales: [const Locale('zh'), const Locale('zh', 'CN')],
|
supportedLocales: [const Locale('zh'), const Locale('zh', 'CN')],
|
||||||
locale: Locale('zh', 'CN'),
|
locale: Locale('zh', 'CN'),
|
||||||
theme: themeProvider.currentThemeData,
|
theme: themeProvider.currentThemeData,
|
||||||
home: const MainScreen(),
|
home: const Home(),
|
||||||
debugShowCheckedModeBanner: false,
|
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:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
import 'package:timezone/data/latest_all.dart' as tz;
|
import 'package:timezone/data/latest_all.dart' as tz;
|
||||||
import 'package:timezone/timezone.dart' as tz;
|
import 'package:timezone/timezone.dart' as tz;
|
||||||
@@ -45,6 +51,51 @@ class NotifyService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await _notifications.initialize(settings);
|
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通知详情
|
// 创建Android通知详情
|
||||||
|
|||||||
@@ -278,8 +278,8 @@ packages:
|
|||||||
flutter_common:
|
flutter_common:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
path: "D:\\Projects\\FlutterProjects\\flutter_common"
|
path: "../flutter_common"
|
||||||
relative: false
|
relative: true
|
||||||
source: path
|
source: path
|
||||||
version: "1.0.0+1"
|
version: "1.0.0+1"
|
||||||
flutter_form_builder:
|
flutter_form_builder:
|
||||||
|
|||||||
58
pubspec.yaml
58
pubspec.yaml
@@ -1,57 +1 @@
|
|||||||
name: flisp_app
|
name: flisp_app
|
||||||
description: "闪灵"
|
|
||||||
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
|
|
||||||
|
|
||||||
version: 1.0.0+2
|
|
||||||
|
|
||||||
environment:
|
|
||||||
sdk: ^3.7.0
|
|
||||||
|
|
||||||
dependencies:
|
|
||||||
flutter:
|
|
||||||
sdk: flutter
|
|
||||||
flutter_localizations:
|
|
||||||
sdk: flutter
|
|
||||||
# The following adds the Cupertino Icons font to your application.
|
|
||||||
# Use with the CupertinoIcons class for iOS style icons.
|
|
||||||
cupertino_icons: ^1.0.8
|
|
||||||
# 状态管理
|
|
||||||
provider: ^6.1.1
|
|
||||||
# 切换开关组件
|
|
||||||
toggle_switch: ^2.3.0
|
|
||||||
# 表单构建器
|
|
||||||
flutter_form_builder: ^10.0.0
|
|
||||||
# 表单验证器
|
|
||||||
form_builder_validators: ^10.0.0
|
|
||||||
# 国际化支持
|
|
||||||
intl: ^0.19.0
|
|
||||||
# 轻量级NoSQL数据库
|
|
||||||
hive: ^2.2.3
|
|
||||||
# Hive的Flutter集成
|
|
||||||
hive_flutter: ^1.1.0
|
|
||||||
# 路径提供器
|
|
||||||
path_provider: ^2.1.1
|
|
||||||
# 本地通知
|
|
||||||
flutter_local_notifications: ^18.0.0
|
|
||||||
# 时区支持
|
|
||||||
timezone: ^0.10.1
|
|
||||||
file_picker: ^10.3.3
|
|
||||||
syncfusion_flutter_calendar: ^30.1.37
|
|
||||||
syncfusion_localizations: ^30.1.37
|
|
||||||
package_info_plus: ^8.0.0
|
|
||||||
flutter_common:
|
|
||||||
path: ..\flutter_common
|
|
||||||
|
|
||||||
dev_dependencies:
|
|
||||||
flutter_test:
|
|
||||||
sdk: flutter
|
|
||||||
hive_generator: ^2.0.1
|
|
||||||
build_runner: ^2.4.6
|
|
||||||
flutter_lints: ^5.0.0
|
|
||||||
|
|
||||||
flutter:
|
|
||||||
uses-material-design: true
|
|
||||||
fonts:
|
|
||||||
- family: CustomFont
|
|
||||||
fonts:
|
|
||||||
- asset: fonts/custom.ttf
|
|
||||||
Reference in New Issue
Block a user