From 1a5efb1b48511dc7eb7566091369ba0b42d2ab24 Mon Sep 17 00:00:00 2001 From: Cxx0822 <1556464090@qq.com> Date: Mon, 24 Nov 2025 11:58:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=90=AF?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=90=8E=E6=B2=A1=E6=9C=89=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=8F=90=E9=86=92=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/layout/home.dart | 55 +++++++++++++++++++ lib/layout/main_screen.dart | 102 ------------------------------------ lib/main.dart | 4 +- lib/utils/notify_utils.dart | 51 ++++++++++++++++++ pubspec.lock | 4 +- pubspec.yaml | 58 +------------------- 6 files changed, 111 insertions(+), 163 deletions(-) create mode 100644 lib/layout/home.dart delete mode 100644 lib/layout/main_screen.dart diff --git a/lib/layout/home.dart b/lib/layout/home.dart new file mode 100644 index 0000000..dd50f11 --- /dev/null +++ b/lib/layout/home.dart @@ -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 createState() => _HomeState(); +} + +class _HomeState extends State { + final GlobalKey _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(); + + 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), + ), + ); + } +} diff --git a/lib/layout/main_screen.dart b/lib/layout/main_screen.dart deleted file mode 100644 index d0d5a16..0000000 --- a/lib/layout/main_screen.dart +++ /dev/null @@ -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 createState() => _MainScreenState(); -} - -class _MainScreenState extends State { - final GlobalKey _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 todoResult = todoService.getAllTodos( - TodoSortMode.priority, - ); - final List 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 calendarResult = calendarService.getAllCalendars(); - final List 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(); - - 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), - ), - ); - } -} diff --git a/lib/main.dart b/lib/main.dart index c74c30d..70aedd6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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, ); } diff --git a/lib/utils/notify_utils.dart b/lib/utils/notify_utils.dart index 93e5f17..4e110e4 100644 --- a/lib/utils/notify_utils.dart +++ b/lib/utils/notify_utils.dart @@ -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 todoResult = todoService.getAllTodos( + TodoSortMode.priority, + ); + final List 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 calendarResult = calendarService.getAllCalendars(); + final List 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通知详情 diff --git a/pubspec.lock b/pubspec.lock index 66a1133..235f1c4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -278,8 +278,8 @@ packages: flutter_common: dependency: "direct main" description: - path: "D:\\Projects\\FlutterProjects\\flutter_common" - relative: false + path: "../flutter_common" + relative: true source: path version: "1.0.0+1" flutter_form_builder: diff --git a/pubspec.yaml b/pubspec.yaml index f28e07c..58a4858 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,57 +1 @@ -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 +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 \ No newline at end of file