Reputation: 336
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
Reputation: 425
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
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
The descriptive/precise documentation is still in progress for the flutter team itself. Many more features to be included soon.
Upvotes: 1