MarvelousWololo
MarvelousWololo

Reputation: 336

How to minimize my desktop Flutter app to system tray?

I am building a Flutter desktop application and I would like to leave the app running on the system tray instead of just closing it.

Upvotes: 10

Views: 4013

Answers (2)

kamalraj venkatesan
kamalraj venkatesan

Reputation: 425

With help of window_manager you can hide the window instead of closing it.

in main.dart

import 'package:flutter/material.dart';
import 'package:window_manager/window_manager.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // Must add this line.
  await windowManager.ensureInitialized();

  WindowOptions windowOptions = WindowOptions(
    size: Size(800, 600),
    center: true,
    backgroundColor: Colors.transparent,
    skipTaskbar: false,
    titleBarStyle: TitleBarStyle.hidden,
  );
  windowManager.waitUntilReadyToShow(windowOptions, () async {
    await windowManager.show();
  });

  runApp(MyApp());
}

To Hide the app window for MacOS, return false on applicationShouldTerminateAfterLastWindowClosed import Cocoa import FlutterMacOS

@NSApplicationMain
class AppDelegate: FlutterAppDelegate {
  override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
     return false
  }
}

call windowManager.hide() when you want to hide the window.

call windowManager.show() when you want to show the window

void _hideWindow() {
  windowManager.hide() // will hide the window and the app will be running in the background
}

void _showWindow() {
  windowManager.show() // will show the window
}

Upvotes: 2

krishna_tandon
krishna_tandon

Reputation: 393

While working on for your query, I found the following links that might be helpful

https://github.com/google/flutter-desktop-embedding/issues/595

https://medium.com/stuart-engineering/%EF%B8%8F-the-tricky-task-of-keeping-flutter-running-on-android-2d51bbc60882

The descriptive/precise documentation is still in progress for the flutter team itself. Many more features to be included soon.

Upvotes: 1

Related Questions