Listen to tray events using TrayListener
mainTo respond to user interactions with the tray (like clicking the icon or selecting a menu item), implement the TrayListener mixin in your StatefulWidget's state.
- Add the listener:
trayManager.addListener(this);ininitState. - Remove the listener:
trayManager.removeListener(this);indispose. - Override methods like
onTrayIconMouseDownoronTrayMenuItemClickto handle logic.
import 'package:flutter/material.dart';
import 'package:tray_manager/tray_manager.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with TrayListener {
@override
void initState() {
trayManager.addListener(this);
super.initState();
_init();
}
@override
void dispose() {
trayManager.removeListener(this);
super.dispose();
}
void _init() {
// ...
}
@override
Widget build(BuildContext context) {
return Container();
}
@override
void onTrayIconMouseDown() {
// example: pop up the menu on click
trayManager.popUpContextMenu();
}
@override
void onTrayIconRightMouseDown() {
// handle right click
}
@override
void onTrayIconRightMouseUp() {
// handle right mouse up
}
@override
void onTrayMenuItemClick(MenuItem menuItem) {
if (menuItem.key == 'show_window') {
// handle show window
} else if (menuItem.key == 'exit_app') {
// handle exit
}
}
}