Enhancing Your Flutter Desktop App with System Tray Support

Aug 1, 2023 | Programming

Welcome, developers! Today, we’re diving into an exciting Flutter package, the system_tray, that brings system tray menu functionality to your desktop applications across Windows, macOS, and Linux. Let’s walk through how to integrate this handy feature into your project seamlessly.

Installation Steps

To get started, you need to add the system tray package to your Flutter project. Here’s how:

  • Open your pubspec.yaml file.
  • Add the following dependency:
  • dependencies:
      ...
      system_tray: ^2.0.3
  • After that, include the import in your Dart library:
  • import 'package:system_tray/system_tray.dart';

Prerequisites for Linux

Linux users, you’re in for a treat! You need to install the following packages:

  • For general usage, run:
  • sudo apt-get install appindicator3-0.1 libappindicator3-dev
  • For Ubuntu 22.04 or greater, execute:
  • sudo apt-get install libayatana-appindicator3-dev

Example Application Setup

Check out the screenshots below to see how the system tray looks on various operating systems:

  • Windows:
  • macOS:
  • Linux:

Understanding the API

The system tray API includes various methods you’d want to utilize:

  • initSystemTray: Initializes the system tray.
  • setSystemTrayInfo: Modifies the tray info including title, icon, and tooltip.
  • setImage: Updates the tray image.
  • setTooltip: Changes the tray tooltip.
  • setTitle / getTitle: Set or get the tray title.
  • setContextMenu: Defines the context menu for the tray.
  • popUpContextMenu: Displays the context menu.
  • destroy: Dismantles the tray.
  • registerSystemTrayEventHandler: Handles tray events such as clicks and right-clicks.

Coding Example

Now, let’s break down a simple yet effective code snippet to manage your system tray:

Future initSystemTray() async {
  String path = Platform.isWindows ? 'assets/app_icon.ico' : 'assets/app_icon.png';
  final AppWindow appWindow = AppWindow();
  final SystemTray systemTray = SystemTray();
  
  // Initialize the system tray menu
  await systemTray.initSystemTray(title: 'system tray', iconPath: path);
  
  // Create context menu
  final 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 the context menu
  await systemTray.setContextMenu(menu);
  
  // Handle system tray events
  systemTray.registerSystemTrayEventHandler((eventName) {
    debugPrint(eventName: $eventName);
    if (eventName == kSystemTrayEventClick) {
      Platform.isWindows ? appWindow.show() : systemTray.popUpContextMenu();
    } else if (eventName == kSystemTrayEventRightClick) {
      Platform.isWindows ? systemTray.popUpContextMenu() : appWindow.show();
    }
  });
}

To put it simply, think of your system tray as a digital clerk at your desk. You instruct the clerk to perform certain tasks when different events happen, like displaying a menu whenever you right-click or showing the window when you click on the icon. This code snippet effectively sets up your digital clerk’s job description!

Troubleshooting

If you encounter issues such as compilation errors listing undefined symbols, here’s how to address that:

  • For errors like Undefined symbols for architecture x86_64, add libc++.tbd to your project.
  • To do that, follow these steps:
    1. Open example/macos/Runner.xcodeproj.
    2. Add libc++.tbd to the ‘Link Binary With Libraries’ in the TARGET runner.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Additional Resources

Here are some recommended libraries that can enhance your window control experience:

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Conclusion

By implementing the system_tray package, you enhance your Flutter app with a powerful feature that can drastically improve user interaction. Enjoy experimenting, and happy coding!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox