This plugin allows Flutter desktop apps to resizing and repositioning the window.
English | 简体中文
- window_manager
- Platform Support
- Quick Start
- Who's using it?
- API
- WindowManager
- Methods
- close
- isPreventClose
- setPreventClose
- focus
- blur
macos
windows
- isFocused
macos
windows
- show
- hide
- isVisible
- isMaximized
- maximize
- unmaximize
- isMinimized
- minimize
- restore
- isFullScreen
- setFullScreen
- setAspectRatio
- setBackgroundColor
- getBounds
- setBounds
- getPosition
- setAlignment
- center
- setPosition
- getSize
- setSize
- setMinimumSize
- setMaximumSize
- isResizable
- setResizable
- isMovable
macos
- setMovable
macos
- isMinimizable
macos
windows
- setMinimizable
macos
windows
- isClosable
macos
windows
- setClosable
macos
windows
- isAlwaysOnTop
- setAlwaysOnTop
- getTitle
- setTitle
- setTitleBarStyle
macos
windows
- getTitleBarHeight
macos
windows
- setSkipTaskbar
- setProgressBar
macos
- hasShadow
macos
- setHasShadow
macos
- getOpacity
macos
windows
- setOpacity
macos
windows
- startDragging
- Methods
- WindowListener
- WindowManager
- License
Linux | macOS | Windows |
---|---|---|
✔️ | ✔️ | ✔️ |
Add this to your package's pubspec.yaml
file:
dependencies:
window_manager: ^0.1.7
Or
dependencies:
window_manager:
git:
url: https://github.com/leanflutter/window_manager.git
ref: main
import 'package:flutter/material.dart';
import 'package:window_manager/window_manager.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Must add this line.
await windowManager.ensureInitialized();
// Use it only after calling `hiddenWindowAtLaunch`
windowManager.waitUntilReadyToShow().then((_) async{
// Hide window title bar
await windowManager.setTitleBarStyle('hidden');
await windowManager.setSize(Size(800, 600));
await windowManager.center();
await windowManager.show();
await windowManager.setSkipTaskbar(false);
});
runApp(MyApp());
}
Please see the example app of this plugin for a full example.
import 'package:flutter/cupertino.dart';
import 'package:window_manager/window_manager.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with WindowListener {
@override
void initState() {
windowManager.addListener(this);
super.initState();
}
@override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
// ...
}
@override
void onWindowEvent(String eventName) {
print('[WindowManager] onWindowEvent: $eventName');
}
@override
void onWindowClose() {
// do something
}
@override
void onWindowFocus() {
// do something
}
@override
void onWindowBlur() {
// do something
}
@override
void onWindowMaximize() {
// do something
}
@override
void onWindowUnmaximize() {
// do something
}
@override
void onWindowMinimize() {
// do something
}
@override
void onWindowRestore() {
// do something
}
@override
void onWindowResize() {
// do something
}
@override
void onWindowMove() {
// do something
}
@override
void onWindowEnterFullScreen() {
// do something
}
@override
void onWindowLeaveFullScreen() {
// do something
}
}
If you need to use the hide method, you need to disable QuitOnClose
.
Change the file macos/Runner/AppDelegate.swift
as follows:
import Cocoa
import FlutterMacOS
@NSApplicationMain
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
- return true
+ return false
}
}
Change the file windows/runner/main.cpp
as follows:
int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
_In_ wchar_t *command_line, _In_ int show_command) {
// ...
- window.SetQuitOnClose(true);
+ window.SetQuitOnClose(false);
// ...
return EXIT_SUCCESS;
}
import 'package:flutter/cupertino.dart';
import 'package:window_manager/window_manager.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with WindowListener {
@override
void initState() {
windowManager.addListener(this);
super.initState();
}
void _init() async {
// Add this line to override the default close handler
await windowManager.setPreventClose(true);
setState(() {});
}
@override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
// ...
}
@override
void onWindowClose() async {
bool _isPreventClose = await windowManager.isPreventClose();
if (_isPreventClose) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text('Are you sure you want to close this window?'),
actions: [
TextButton(
child: Text('No'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text('Yes'),
onPressed: () {
Navigator.of(context).pop();
exit(0);
},
),
],
);
},
);
}
}
}
Hidden at launch
Change the file macos/Runner/MainFlutterWindow.swift
as follows:
import Cocoa
import FlutterMacOS
+import window_manager
class MainFlutterWindow: NSWindow {
override func awakeFromNib() {
let flutterViewController = FlutterViewController.init()
let windowFrame = self.frame
self.contentViewController = flutterViewController
self.setFrame(windowFrame, display: true)
RegisterGeneratedPlugins(registry: flutterViewController)
super.awakeFromNib()
}
+ override public func order(_ place: NSWindow.OrderingMode, relativeTo otherWin: Int) {
+ super.order(place, relativeTo: otherWin)
+ hiddenWindowAtLaunch()
+ }
}
Change the file windows/runner/win32_window.cpp
as follows:
bool Win32Window::CreateAndShow(const std::wstring& title,
const Point& origin,
const Size& size) {
...
HWND window = CreateWindow(
- window_class, title.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE,
+ window_class, title.c_str(),
+ WS_OVERLAPPEDWINDOW, // do not add WS_VISIBLE since the window will be shown later
Scale(origin.x, scale_factor), Scale(origin.y, scale_factor),
Scale(size.width, scale_factor), Scale(size.height, scale_factor),
nullptr, nullptr, GetModuleHandle(nullptr), this);
Make sure to call setState
once on the onWindowFocus
event.
import 'package:flutter/cupertino.dart';
import 'package:window_manager/window_manager.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with WindowListener {
@override
void initState() {
windowManager.addListener(this);
super.initState();
}
@override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
// ...
}
@override
void onWindowFocus() {
// Make sure to call once.
setState(() {});
// do something
}
}
- AuthPass - Password Manager based on Flutter for all platforms. Keepass 2.x (kdbx 3.x) compatible.
- Biyi (比译) - A convenient translation and dictionary app written in dart / Flutter.
- BlueBubbles - BlueBubbles is an ecosystem of apps bringing iMessage to Android, Windows, and Linux
- Yukino - Yukino lets you read manga or stream anime ad-free from multiple sources.
- LunaSea - A self-hosted controller for mobile and macOS built using the Flutter framework.
Try to close the window.
Check if is intercepting the native close signal.
Set if intercept the native close signal. May useful when combine with the onclose event listener. This will also prevent the manually triggered close event.
Focuses on the window.
Removes focus from the window.
Returns bool
- Whether window is focused.
Shows and gives focus to the window.
Hides the window.
Returns bool
- Whether the window is visible to the user.
Returns bool
- Whether the window is maximized.
Maximizes the window.
Unmaximizes the window.
Returns bool
- Whether the window is minimized.
Minimizes the window. On some platforms the minimized window will be shown in the Dock.
Restores the window from minimized state to its previous state.
Returns bool
- Whether the window is in fullscreen mode.
Sets whether the window should be in fullscreen mode.
This will make a window maintain an aspect ratio.
Sets the background color of the window.
Returns Rect
- The bounds of the window as Object.
Resizes and moves the window to the supplied bounds.
Returns Offset
- Contains the window's current position.
Move the window to a position aligned with the screen.
Moves window to the center of the screen.
Moves window to position.
Returns Size
- Contains the window's width and height.
Resizes the window to width
and height
.
Sets the minimum size of window to width
and height
.
Sets the maximum size of window to width
and height
.
Returns bool
- Whether the window can be manually resized by the user.
Sets whether the window can be manually resized by the user.
Returns bool
- Whether the window can be moved by user.
Sets whether the window can be moved by user.
Returns bool
- Whether the window can be manually minimized by the user.
Sets whether the window can be manually minimized by user.
Returns bool
- Whether the window can be manually closed by user.
Sets whether the window can be manually closed by user.
Returns bool
- Whether the window is always on top of other windows.
Sets whether the window should show always on top of other windows.
Returns String
- The title of the native window.
Changes the title of native window to title.
Changes the title bar style of native window.
Returns int
- The title bar height of the native window.
Makes the window not show in the taskbar / dock.
Sets progress value in progress bar. Valid range is [0, 1.0].
Returns bool
- Whether the window has a shadow.
Sets whether the window should have a shadow.
Returns double
- between 0.0 (fully transparent) and 1.0 (fully opaque). On Linux, always returns 1.
Sets the opacity of the window.
Starts a window drag based on the specified mouse-down event.
Emitted when the window is going to be closed.
Emitted when the window gains focus.
Emitted when the window loses focus.
Emitted when window is maximized.
Emitted when the window exits from a maximized state.
Emitted when the window is minimized.
Emitted when the window is restored from a minimized state.
Emitted after the window has been resized.
Emitted when the window is being moved to a new position.
Emitted when the window enters a full-screen state.
Emitted when the window leaves a full-screen state.
Emitted all events.