Flutter桌面软件开发中实现本地通知

Flutter桌面软件开发中实现本地通知可以使用local_notifier ,local_notifier这个插件允许 Flutter 桌面 应用显示本地通知。

Flutter桌面软件开发中实现本地通知 第一步安装依赖

复制代码
dependencies:
  local_notifier: ^0.1.5

Flutter桌面软件开发中实现本地通知 第二步配置插件

1、main方法配置

复制代码
void main() async{
  runApp(const MyApp());
  //配置异步通知
  await localNotifier.setup(
    appName: 'IT营',
    // 参数 shortcutPolicy 仅适用于 Windows
    shortcutPolicy: ShortcutPolicy.requireCreate,
  );
}

2、用到的地方弹窗

复制代码
LocalNotification notification = LocalNotification(
                    title: "local_notifier_example",
                    body: "hello flutter!",
);

Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: () {
                  //通知完结

                  LocalNotification notification = LocalNotification(
                    title: "local_notifier_example",
                    body: "hello flutter!",
                  );

                  notification.onShow = () {
                    print('onShow ${notification.identifier}');
                  };
                  notification.onClose = (closeReason) {
                    // 只支持在windows,其他平台 closeReason 始终为 unknown。
                    switch (closeReason) {
                      case LocalNotificationCloseReason.userCanceled:
                        // do something
                        break;
                      case LocalNotificationCloseReason.timedOut:
                        // do something
                        break;
                      default:
                    }
                    print('onClose  - $closeReason');
                  };
                  notification.onClick = () {
                    print('onClick ${notification.identifier}');
                  };
                  notification?.onClickAction = (actionIndex) {
                    print(
                        'onClickAction ${notification?.identifier} - $actionIndex');
                  };
                  notification.show();
                },
                child: Text("显示notification"))
          ],
        ),
      ),
    );
  }
相关推荐
火柴就是我38 分钟前
从头写一个自己的app
android·前端·flutter
●VON7 小时前
Flutter 项目成功运行后,如何正确迁移到 OpenHarmony?常见疑问与跳转失效问题解析
flutter·华为·openharmony·开源鸿蒙
●VON8 小时前
Flutter 编译开发 OpenHarmony 全流程实战教程(基于 GitCode 社区项目)
flutter·openharmony·gitcode
消失的旧时光-194320 小时前
Flutter 组件:Row / Column
flutter
程序员老刘1 天前
Flutter版本选择指南:3.35稳定,3.38发布 | 2025年11月
flutter·客户端
kirk_wang1 天前
Flutter 3.38和Dart 3.10中最大的更新
flutter
前端小伙计1 天前
Flutter 配置国内镜像,加速项目加载!
flutter
zonda的地盘1 天前
开发 Flutter Plugin 之 初始配置
flutter
消失的旧时光-19432 天前
Flutter TextField 从入门到精通:掌握输入框的完整指南
flutter
wordbaby2 天前
Flutter Form Builder 完全指南:告别 Controller 地狱
前端·flutter