Orientação gerada a partir de informações públicas. Apenas para referência.
O pacote tray_manager permite que aplicativos desktop do Flutter integrem funcionalidades de bandeja do sistema no macOS, Windows e Linux. Ele permite aos desenvolvedores criar e gerenciar ícones na bandeja do sistema, lidar com interações do usuário como cliques e menus contextuais, e exibir notificações. Isso é particularmente útil para aplicativos que precisam rodar em segundo plano, mas permanecer acessíveis pela bandeja do sistema. O pacote fornece uma API consistente entre plataformas, simplificando o desenvolvimento multiplataforma para desktop.
Casos de uso
Aplicativos em segundo plano com interface mínima
Ferramentas de monitoramento do sistema
Centros de notificações
Acesso rápido aos recursos do aplicativo
Utilitários de desktop
Presença persistente do aplicativo
Principais recursos
Suporte de bandeja multiplataforma
Ícones de bandeja personalizáveis
Manipulação de eventos de clique e clique com botão direito
Integração do menu de contexto
API simples para aplicativos de desktop
Suporta macOS, Windows e Linux
Indicado para
Desenvolvedores criando aplicativos desktop-only do Flutter
Aplicativos que exigem presença persistente no sistema
Ferramentas que precisam de acesso rápido sem janelas completas
Serviços em segundo plano com interação do usuário
Aplicações de desktop minimalistas
Considerações
Requer destino a plataforma de desktop
Limitado à bandeja do sistema, não ao gerenciamento completo da janela
Comportamentos específicos da plataforma podem variar
Nenhum centro de notificação embutido além de alertas básicos
Não adequado para aplicativos móveis ou da web
README original
Snapshot do README original do repositório-fonte vinculado. Consulte o repositório para o conteúdo mais recente.
On Linux the tray icon is a StatusNotifierItem, which needs a panel that hosts them. KDE Plasma and most other desktops do; GNOME does only with the AppIndicator extension (Ubuntu ships it enabled).
Flutter 3.47 / Dart 3.13 or later, macOS 10.15 or later.
Linux build machines need GTK 3, X11 and Xi development files. The tray icon is a
StatusNotifierItem over D-Bus, so libayatana-appindicator3-dev is no longer needed.
import'package:tray_manager/tray_manager.dart';
final trayIcon = TrayIcon.create()!;
trayIcon.icon = ImageAsset.fromAsset('images/tray_icon.png');
trayIcon.setTooltip('tray_manager');
final menu = Menu.create()!;
final showWindowItem = MenuItem.createWithLabelAndType(
'Show Window',
MenuItemType.normal,
)!;
showWindowItem.addListener((event) {
if (event isMenuItemClickedEvent) {
// Show the application window.
}
});
menu.addItem(showWindowItem);
menu.addSeparator();
menu.addItem(MenuItem.createWithLabelAndType('Exit App', MenuItemType.normal));
trayIcon.setContextMenu(menu);
trayIcon.setVisible(true);
The example app of this plugin covers the 0.5.x compatible API. For the
full example — several icons, animated icons, every native property — see nativeapi's
tray_icon_example.
Upgrading from 0.5.x
Code written for tray_manager 0.5.x keeps working by importing
package:tray_manager/legacy.dart instead of package:tray_manager/tray_manager.dart.
It provides the old trayManager, TrayListener, Menu and MenuItem
(menu_base is no longer a dependency) on top of the native API.
The import has to change on purpose: legacy.dart is a bridge, not the future of this
package. Its classes are marked @Deprecated and will be removed in a later
release — move to the native API above when you can.
Builds need Flutter 3.47 / Dart 3.13 and macOS 10.15 (0.5.x: Flutter 3.3, macOS 10.11).
A click is reported when it completes, as onTrayIconMouseDown immediately followed by
onTrayIconMouseUp (same for the right button). 0.5.x sent the two separately on macOS
and only the first on Windows. Linux still reports no tray icon clicks at all: the
panel keeps them and opens the menu itself.
Calls a platform has no use for are ignored instead of throwing
MissingPluginException: setTitle and setIconPosition on Windows; setIconPosition,
popUpContextMenu and getBounds (which answers null) on Linux. setToolTip now
works on Linux.
setIcon throws an ArgumentError when the image cannot be loaded, on every
platform. Windows takes .png as well as .ico now.
bringAppToFront of popUpContextMenu is accepted but ignored.
MenuItem.onClick runs once per click, also when no TrayListener is
registered.
label, toolTip, checked and disabled of a MenuItem update the
visible menu as soon as they are assigned; adding or removing items still
needs another setContextMenu call.
sublabel, onHighlight and onLoseHighlight are kept but unused.
Moving to the native API
0.5.x (legacy.dart)
Native API (tray_manager.dart)
trayManager (one icon per app)
TrayIcon.create() — as many as you need; keep the object, dispose() it when done
item.addListener((event) { if (event is MenuItemClickedEvent) ... }) per item
popUpContextMenu() from onTrayIconRightMouseDown
trayIcon.setContextMenuTrigger(ContextMenuTrigger.rightClicked), or trayIcon.openContextMenu()
TrayListener
trayIcon.addListener((event) { switch (event) { case TrayIconClickedEvent(): ... } }) — also TrayIconRightClickedEvent, TrayIconDoubleClickedEvent (none of them on Linux)
getBounds()
trayIcon.getBounds() (synchronous; physical pixels on Windows, an empty Rect on Linux)
destroy()
trayIcon.dispose()
Keep a reference to every TrayIcon for as long as it should be shown: a wrapper that is
garbage-collected releases its native handle, and that removes the icon. A Menu or
MenuItem that is attached stays alive natively, but keep the ones you want to change
later.
Who's using it?
Airclap - Send any file to any device. cross platform, ultra fast and easy to use.
Biyi (比译) - A convenient translation and dictionary app.
tray_manager now re-exports tray-related APIs from nativeapi, including
TrayIcon, TrayManager, Menu, MenuItem, Image, and tray/menu events.
Import package:tray_manager/legacy.dart only for code that still uses the
0.5.x API.