antler119 / system_tray

A Flutter package that makes it easy to customize and work with your Flutter desktop app's system tray.
MIT License
214 stars 51 forks source link

Lost connection to device when MenuItem is clicked #45

Open huskyjp opened 2 years ago

huskyjp commented 2 years ago

I am facing some issues that flutter always terminate app running with Lost connection to device. when I tap any MenuItem.

Since I do not want to show any window, I do not use win.show() in my doWhenWindowReady initialization function. Here is the snippet of my code.

import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:flutter/material.dart' hide MenuItem;
import 'package:system_tray/system_tray.dart';

import 'package:url_launcher/url_launcher.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MenuBar());
}

class MenuBar extends StatefulWidget {
  const MenuBar({Key? key}) : super(key: key);

  @override
  State<MenuBar> createState() => _MenuBarState();
}

class _MenuBarState extends State<MenuBar> {
  final SystemTray _tray = SystemTray();
  final AppWindow _appWindow = AppWindow();
  final Uri _url = Uri.parse('https://google.com/');

  @override
  void initState() {
    super.initState();
    doWhenWindowReady(() {
      initSystemTray();
      // final win = appWindow;
      // const initialSize = Size(600, 450);
      // win.minSize = initialSize;
      // win.size = initialSize;
      // win.alignment = Alignment.center;
      // win.title = "How to use system tray with Flutter";
      // win.show();
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  Future<void> initSystemTray() async {
    final menu = [
      MenuItem(
          label: 'Login',
          onClicked: () async {
            print('clicked');
            _launchUrl();
          }),
      MenuSeparator(),
      MenuItem(label: 'About', onClicked: _appWindow.hide),
      MenuSeparator(),
      MenuItem(label: 'Quit', onClicked: _appWindow.close),
    ];

    await _tray.initSystemTray(title: '', iconPath: 'assets/Logo.png');

    // assign tray menus
    await _tray.setContextMenu(menu);

    _tray.registerSystemTrayEventHandler((eventName) {
      debugPrint("eventName: $eventName");
      if (eventName == "lefMouseDown") {
      } else if (eventName == "leftMouseUp") {
        _tray.popUpContextMenu();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }

  void _launchUrl() async {
    if (!await launchUrl(_url)) throw 'Could not launch $_url';
  }
}

When I initialize win.show(); in my first init function, flutter doesn't terminate my app running and works as desired. However, since I do not need any window UI and just want to use the system tray. Any way to achieve this or a solution?

pateljoel commented 2 years ago

Having the same issue too, I person pointed me here and told me this issue was fixed, but not so sure.

@antler119 Is there any solution or workaround without an app window?