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 仅适用于 WindowsshortcutPolicy: 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 somethingbreak;case LocalNotificationCloseReason.timedOut:// do somethingbreak;default:}print('onClose - $closeReason');};notification.onClick = () {print('onClick ${notification.identifier}');};notification?.onClickAction = (actionIndex) {print('onClickAction ${notification?.identifier} - $actionIndex');};notification.show();},child: Text("显示notification"))],),),);}