flutter 桌面应用之系统托盘

 系统托盘(Tray)

系统托盘就是状态栏里面对应的图标点击菜单


 

主要有两款框架

框架一句话评价
tray_manager轻量、简单、易用,适合常规托盘功能
system_tray更底层、更强大、支持图标/菜单/消息弹窗等更多功能,但复杂度更高

🧱 基础能力对比

能力tray_managersystem_tray
添加系统托盘图标
托盘菜单支持✅(基本菜单)✅(更丰富,支持子菜单、图标)
点击托盘事件监听✅(click, right click 等)✅(支持更多系统原生事件)
更改托盘图标
弹出系统原生消息(Balloon)✅(仅 Windows 支持)
子菜单图标/多级菜单
多平台支持(Win/macOS/Linux)✅(全支持)✅(全支持)
桌面通知✅(Windows balloon)
设置 Tooltip

⚙️ 技术架构对比

属性tray_managersystem_tray
底层语言通过 Dart FFI 调用 C++/Objective-C更底层,直接使用 C/C++ 实现系统调用
依赖框架flutter, ffiflutter, ffi, tray_system(C库)
项目大小小、纯 Flutter稍大,构建依赖更复杂
接入简单性更简单,API 清晰功能强大但 API 比较底层,写法偏原生

🔧 示例代码对比

(添加托盘图标 + 菜单)

tray_manager 简洁示例:

await trayManager.setIcon('assets/tray_icon.png');
await trayManager.setContextMenu(Menu(items: [MenuItem(key: 'show', label: 'Show Window'),MenuItem.separator(),MenuItem(key: 'exit', label: 'Exit'),
]));trayManager.addListener(MyTrayListener());

system_tray 强大示例:

final SystemTray tray = SystemTray();
await tray.initSystemTray(iconPath: 'assets/tray_icon.png',toolTip: 'My Tray App',
);final Menu menu = Menu();
await menu.buildFrom([MenuItemLabel(label: 'Show Window'),MenuSeparator(),MenuItemLabel(label: 'Exit'),
]);await tray.setContextMenu(menu);
tray.registerSystemTrayEventHandler((eventName) {print("Tray event: $eventName");
});

✅ 推荐场景(选型建议)

你的需求推荐插件理由
只要托盘图标 + 简单菜单tray_manager简单好用,集成快
要显示系统消息、子菜单、图标菜单system_tray功能强大,系统级集成更全面
要更复杂的原生交互(例如原生通知)system_tray支持 Windows Balloon/原生消息
想快速开发 MVP,功能不复杂tray_manager更容易快速上线
在意插件活跃度 / 维护频率system_trayJetBrains 社区更认可(大佬多)

hello word

import 'dart:io';import 'package:flutter/material.dart';
import 'package:system_tray/system_tray.dart';void main() {runApp(const MyApp());initSystemTray();
}Future<void> initSystemTray() async {String path =Platform.isWindows ? 'assets/app_icon.ico' : 'assets/app_icon.png';final AppWindow appWindow = AppWindow();final SystemTray systemTray = SystemTray();// We first init the systray menuawait systemTray.initSystemTray(title: "system tray",iconPath: path,);// create context menufinal Menu menu = Menu();await menu.buildFrom([MenuItemLabel(label: 'Show', onClicked: (menuItem) => appWindow.show()),MenuItemLabel(label: 'Hide', onClicked: (menuItem) => appWindow.hide()),MenuItemLabel(label: 'Exit', onClicked: (menuItem) => appWindow.close()),]);// set context menuawait systemTray.setContextMenu(menu);// handle system tray eventsystemTray.registerSystemTrayEventHandler((eventName) {debugPrint("eventName: $eventName");if (eventName == kSystemTrayEventClick) {Platform.isWindows ? appWindow.show() : systemTray.popUpContextMenu();} else if (eventName == kSystemTrayEventRightClick) {Platform.isWindows ? systemTray.popUpContextMenu() : appWindow.show();}});
}
class MyApp extends StatelessWidget {const MyApp({super.key});// This widget is the root of your application.@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(// This is the theme of your application.//// TRY THIS: Try running your application with "flutter run". You'll see// the application has a purple toolbar. Then, without quitting the app,// try changing the seedColor in the colorScheme below to Colors.green// and then invoke "hot reload" (save your changes or press the "hot// reload" button in a Flutter-supported IDE, or press "r" if you used// the command line to start the app).//// Notice that the counter didn't reset back to zero; the application// state is not lost during the reload. To reset the state, use hot// restart instead.//// This works for code too, not just values: Most code changes can be// tested with just a hot reload.colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),),home: const MyHomePage(title: 'Flutter Demo Home Page'),);}
}class MyHomePage extends StatefulWidget {const MyHomePage({super.key, required this.title});// This widget is the home page of your application. It is stateful, meaning// that it has a State object (defined below) that contains fields that affect// how it looks.// This class is the configuration for the state. It holds the values (in this// case the title) provided by the parent (in this case the App widget) and// used by the build method of the State. Fields in a Widget subclass are// always marked "final".final String title;@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {int _counter = 0;void _incrementCounter() {setState(() {// This call to setState tells the Flutter framework that something has// changed in this State, which causes it to rerun the build method below// so that the display can reflect the updated values. If we changed// _counter without calling setState(), then the build method would not be// called again, and so nothing would appear to happen._counter++;});}@overrideWidget build(BuildContext context) {// This method is rerun every time setState is called, for instance as done// by the _incrementCounter method above.//// The Flutter framework has been optimized to make rerunning build methods// fast, so that you can just rebuild anything that needs updating rather// than having to individually change instances of widgets.return Scaffold(appBar: AppBar(// TRY THIS: Try changing the color here to a specific color (to// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar// change color while the other colors stay the same.backgroundColor: Theme.of(context).colorScheme.inversePrimary,// Here we take the value from the MyHomePage object that was created by// the App.build method, and use it to set our appbar title.title: Text(widget.title),),body: Center(// Center is a layout widget. It takes a single child and positions it// in the middle of the parent.child: Column(// Column is also a layout widget. It takes a list of children and// arranges them vertically. By default, it sizes itself to fit its// children horizontally, and tries to be as tall as its parent.//// Column has various properties to control how it sizes itself and// how it positions its children. Here we use mainAxisAlignment to// center the children vertically; the main axis here is the vertical// axis because Columns are vertical (the cross axis would be// horizontal).//// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"// action in the IDE, or press "p" in the console), to see the// wireframe for each widget.mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[const Text('You have pushed the button this many times:'),Text('$_counter',style: Theme.of(context).textTheme.headlineMedium,),],),),floatingActionButton: FloatingActionButton(onPressed: _incrementCounter,tooltip: 'Increment',child: const Icon(Icons.add),), // This trailing comma makes auto-formatting nicer for build methods.);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/75621.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

修改idea/android studio等编辑器快捷注释从当前行开头的反人类行为

不知道什么时候开始&#xff0c;idea编辑的快捷注释开始从当前行开头出现了&#xff0c;显得实在是难受&#xff0c;我只想让在当前行代码的部份开始缩进两个字符开始&#xff0c;这样才会显得更舒服。不知道有没有强迫症的猴子和我一样&#xff0c;就像下面的效果&#xff1a;…

MySQL慢查询全攻略:定位、分析与优化实战

&#x1f680; MySQL慢查询全攻略&#xff1a;定位、分析与优化实战 #数据库优化 #性能调优 #SQL优化 #MySQL实战 一、慢查询定位&#xff1a;找到性能瓶颈 1.1 开启慢查询日志 -- 查看当前配置 SHOW VARIABLES LIKE %slow_query%; -- 动态开启&#xff08;重启失效&…

当原型图与文字说明完全不同时,测试要怎么做?

当测试遇上左右手互搏的需求&#xff0c;怎么办&#xff1f; "这个弹窗样式怎么和文档写的不一样&#xff1f;"、"按钮位置怎么跑到左边去了&#xff1f;"——根据Deloitte的调查&#xff0c;62%的项目存在原型图与需求文档不一致的情况。这种"精神分…

关于量化交易在拉盘砸盘方面应用的部分思考

关于“砸盘”的深层解析与操盘逻辑 ​​一、砸盘的本质与市场含义​​ ​​砸盘​​指通过集中抛售大量筹码导致价格快速下跌的行为&#xff0c;其核心目标是​​制造恐慌、清洗浮筹或实现利益再分配​​。不同场景下的砸盘含义不同&#xff1a; ​​主动砸盘&#xff08;操控…

【项目管理】第12章 项目质量管理-- 知识点整理

项目管理-相关文档,希望互相学习,共同进步 风123456789~-CSDN博客 (一)知识总览 项目管理知识域 知识点: (项目管理概论、立项管理、十大知识域、配置与变更管理、绩效域) 对应:第6章-第19章 第6章 项目管理概论 4分第13章 项目资源管理 3-4分第7章 项目…

一个好看的图集展示html页面源码

源码介绍 一个好看的图集展示html页面源码&#xff0c;适合展示自己的作品&#xff0c;页面美观大气&#xff0c;也可以作为产品展示或者个人引导页等等 源码由HTMLCSSJS组成&#xff0c;记事本打开源码文件可以进行内容文字之类的修改&#xff0c; 双击html文件可以本地运行…

2021第十二届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组

记录刷题的过程、感悟、题解。 希望能帮到&#xff0c;那些与我一同前行的&#xff0c;来自远方的朋友&#x1f609; 大纲&#xff1a; 1、空间-&#xff08;题解&#xff09;-字节单位转换 2、卡片-&#xff08;题解&#xff09;-可以不用当组合来写&#xff0c;思维题 3、直…

LabVIEW 中 JSON 数据与簇的转换

在 LabVIEW 编程中&#xff0c;数据格式的处理与转换是极为关键的环节。其中&#xff0c;将数据在 JSON 格式与 LabVIEW 的簇结构之间进行转换是一项常见且重要的操作。这里展示的程序片段就涉及到这一关键功能&#xff0c;以下将详细介绍。 一、JSON 数据与簇的转换功能 &am…

蓝桥杯大模板

init.c void System_Init() {P0 0x00; //关闭蜂鸣器和继电器P2 P2 & 0x1f | 0xa0;P2 & 0x1f;P0 0x00; //关闭LEDP2 P2 & 0x1f | 0x80;P2 & 0x1f; } led.c #include <LED.H>idata unsigned char temp_1 0x00; idata unsigned char temp_old…

通过HTTP协议实现Git免密操作的解决方案

工作中会遇到这样的问题的。 通过HTTP协议实现Git免密操作的解决方案 方法一&#xff1a;启用全局凭据存储&#xff08;推荐&#xff09; 配置凭证存储‌ 执行以下命令&#xff0c;让Git永久保存账号密码&#xff08;首次操作后生效&#xff09;&#xff1a; git config --g…

Java常见面试问题

一.Liunx 二.Java基础 1.final 2.static 3.与equals 三.Collection 1.LIst 2.Map 3.Stream 四、多线程 1.实现方法 2.线程池核心参数 3.应用场景 五、JVM 1.堆 2.栈 六、Spring 1.面向对象 2.IOC 3.AOP 七、Springboot 1.自动装配 八、SpringCloud 1.Nacos 2.seata 3.ga…

【蓝桥杯】第十六届蓝桥杯 JAVA B组记录

试题 A: 逃离高塔 很简单&#xff0c;签到题&#xff0c;但是需要注意精度&#xff0c;用int会有溢出风险 答案&#xff1a;202 package lanqiao.t1;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWrit…

PyTorch Tensor维度变换实战:view/squeeze/expand/repeat全解析

本文从图像数据处理、模型输入适配等实际场景出发&#xff0c;系统讲解PyTorch中view、squeeze、expand和repeat四大维度变换方法。通过代码演示对比不同方法的适用性&#xff0c;助您掌握数据维度调整的核心技巧。 一、基础维度操作方法 1. view&#xff1a;内存连续的形状重…

Kubernetes nodeName Manual Scheduling practice (K8S节点名称绑定以及手工调度)

Manual Scheduling 在 Kubernetes 中&#xff0c;手动调度框架允许您将 Pod 分配到特定节点&#xff0c;而无需依赖默认调度器。这对于测试、调试或处理特定工作负载非常有用。您可以通过在 Pod 的规范中设置 nodeName 字段来实现手动调度。以下是一个示例&#xff1a; apiVe…

即时编译器(JIT)的编译过程是什么?

1. 触发编译 JIT编译的触发基于热点代码检测&#xff0c;主要通过两种计数器&#xff1a; • 方法调用计数器&#xff1a;统计方法被调用的次数&#xff08;默认阈值&#xff1a;C1为1,500次&#xff0c;C2为10,000次&#xff09;。 • 回边计数器&#xff1a;统计循环体的执行…

Java基础:集合List、Map、Set(超详细版)

集合体系概述 Collection常用方法 补充&#xff1a;addAll() Collection的遍历方式 迭代器 增强for&#xff08;空集合可以&#xff0c;null不可以&#xff09; lambda 集合对象存储对象原理 遍历方式的区别 List集合 特点、特有方法 遍历方式 &#xff08;同上&#xff09…

Elasticsearch 全面解析

Elasticsearch 全面解析 前言一、简介核心特性应用场景 二、核心原理与架构设计1. 倒排索引&#xff08;Inverted Index&#xff09;2. 分片与副本机制&#xff08;Sharding & Replication&#xff09;3. 节点角色与集群管理 三、核心特点1. 灵活的查询语言&#xff08;Que…

【2】k8s集群管理系列--包应用管理器之helm(Chart语法深入应用)

一、Chart模板&#xff1a;函数与管道 常用函数&#xff1a; • quote&#xff1a;将值转换为字符串&#xff0c;即加双引号 • default&#xff1a;设置默认值&#xff0c;如果获取的值为空则为默认值 • indent和nindent&#xff1a;缩进字符串 • toYaml&#xff1a;引用一…

JVM 字节码是如何存储信息的?

JVM 字节码是 Java 虚拟机 (JVM) 执行的指令集&#xff0c;它是一种与平台无关的二进制格式&#xff0c;在任何支持 JVM 的平台上都可运行的Java 程序。 字节码存储信息的方式&#xff0c;主要通过以下几个关键组成部分和机制来实现&#xff1a; 1. 指令 (Opcodes) 和 操作数 …

基于51单片机语音实时采集系统

基于51单片机语音实时采集 &#xff08;程序&#xff0b;原理图&#xff0b;PCB&#xff0b;设计报告&#xff09; 功能介绍 具体功能&#xff1a; 系统由STC89C52单片机ISD4004录音芯片LM386功放模块小喇叭LCD1602按键指示灯电源构成 1.可通过按键随时选择相应的录音进行播…