这个插件允许 Flutter 桌面应用调整窗口的大小和位置。
English | 简体中文
- 平台支持
- 快速开始
- 文章
- 谁在用使用它?
- API
- WindowManager
- Methods
- waitUntilReadyToShow
- destroy
- close
- isPreventClose
- setPreventClose
- focus
- blur
macos
windows
- isFocused
macos
windows
- show
- hide
- isVisible
- isMaximized
- maximize
- unmaximize
- isMinimized
- minimize
- restore
- isFullScreen
- setFullScreen
- isDockable
windows
- isDocked
windows
- dock
windows
- undock
windows
- setAspectRatio
- setBackgroundColor
- setAlignment
- center
- getBounds
- setBounds
- getSize
- setSize
- getPosition
- setPosition
- setMinimumSize
- setMaximumSize
- isResizable
- setResizable
- isMovable
macos
- setMovable
macos
- isMinimizable
macos
windows
- setMinimizable
macos
windows
- isClosable
windows
- isMaximizable
macos
windows
- setMaximizable
- setClosable
macos
windows
- isAlwaysOnTop
- setAlwaysOnTop
- isAlwaysOnBottom
- setAlwaysOnBottom
linux
windows
- getTitle
- setTitle
- setTitleBarStyle
- getTitleBarHeight
- isSkipTaskbar
- setSkipTaskbar
- setProgressBar
macos
windows
- setIcon
windows
- isVisibleOnAllWorkspaces
macos
- setVisibleOnAllWorkspaces
macos
- setBadgeLabel
macos
- hasShadow
macos
windows
- setHasShadow
macos
windows
- getOpacity
- setOpacity
- setBrightness
- setIgnoreMouseEvents
- startDragging
- startResizing
linux
windows
- grabKeyboard
linux
- ungrabKeyboard
linux
- Methods
- WindowListener
- Methods
- onWindowClose
- onWindowFocus
- onWindowBlur
- onWindowMaximize
- onWindowUnmaximize
- onWindowMinimize
- onWindowRestore
- onWindowResize
- onWindowResized
macos
windows
- onWindowMove
- onWindowMoved
macos
windows
- onWindowEnterFullScreen
- onWindowLeaveFullScreen
- onWindowDocked
windows
- onWindowUndocked
windows
- onWindowEvent
- Methods
- WindowManager
- 贡献者
- 许可证
Linux | macOS | Windows |
---|---|---|
✔️ | ✔️ | ✔️ |
将此添加到你的软件包的 pubspec.yaml
文件:
dependencies:
window_manager: ^0.4.2
或
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();
// 必须加上这一行。
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();
await windowManager.focus();
});
runApp(MyApp());
}
请看这个插件的示例应用,以了解完整的例子。
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() {
super.initState();
windowManager.addListener(this);
}
@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
}
}
如果你需要使用 hide
方法,你需要禁用 QuitOnClose
。
更改文件 macos/Runner/AppDelegate.swift
如下:
import Cocoa
import FlutterMacOS
@NSApplicationMain
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
- return true
+ return false
}
}
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() {
super.initState();
windowManager.addListener(this);
_init();
}
@override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}
void _init() async {
// 添加此行以覆盖默认关闭处理程序
await windowManager.setPreventClose(true);
setState(() {});
}
@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();
await windowManager.destroy();
},
),
],
);
},
);
}
}
}
更改文件 linux/my_application.cc
如下:
...
// Implements GApplication::activate.
static void my_application_activate(GApplication* application) {
...
gtk_window_set_default_size(window, 1280, 720);
- gtk_widget_show(GTK_WIDGET(window));
+ gtk_widget_realize(GTK_WIDGET(window));
g_autoptr(FlDartProject) project = fl_dart_project_new();
fl_dart_project_set_dart_entrypoint_arguments(project, self->dart_entrypoint_arguments);
FlView* view = fl_view_new(project);
gtk_widget_show(GTK_WIDGET(view));
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view));
fl_register_plugins(FL_PLUGIN_REGISTRY(view));
gtk_widget_grab_focus(GTK_WIDGET(view));
}
...
更改文件 macos/Runner/MainFlutterWindow.swift
如下:
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()
+ }
}
更改文件 windows/runner/win32_window.cpp
如下:
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);
使用 flutter 3.7 创建的 Windows 项目
更改 windows/runner/flutter_window.cpp
如下:
bool FlutterWindow::OnCreate() {
...
flutter_controller_->engine()->SetNextFrameCallback([&]() {
- this->Show();
+ "" //删除 this->Show()
});
确保在 onWindowFocus
事件中调用一次 setState
。
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() {
super.initState();
windowManager.addListener(this);
}
@override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
// ...
}
@override
void onWindowFocus() {
// Make sure to call once.
setState(() {});
// do something
}
}
- Airclap - 任何文件,任意设备,随意发送。简单好用的跨平台高速文件传输APP。
- AuthPass - 基于Flutter的密码管理器,适用于所有平台。兼容Keepass 2.x(kdbx 3.x)。
- Biyi (比译) - 一个便捷的翻译和词典应用程序。
- BlueBubbles - BlueBubbles is an ecosystem of apps bringing iMessage to Android, Windows, and Linux
- LunaSea - A self-hosted controller for mobile and macOS built using the Flutter framework.
- Linwood Butterfly - 用 Flutter 编写的开源笔记应用
- RustDesk - 远程桌面软件,开箱即用,无需任何配置。您完全掌控数据,不用担心安全问题。
- Ubuntu Desktop Installer - This project is a modern implementation of the Ubuntu Desktop installer.
- UniControlHub - Seamlessly bridge your Desktop and Mobile devices
Wait until ready to show.
Force closing the window.
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. vertically
simulates aero snap, only works on Windows
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.
Returns bool
- Whether the window is dockable or not.
Returns bool
- Whether the window is docked.
Docks the window. only works on Windows
Undocks the window. only works on Windows
This will make a window maintain an aspect ratio.
Sets the background color of the window.
Move the window to a position aligned with the screen.
Moves window to the center of the screen.
Returns Rect
- The bounds of the window as Object.
Resizes and moves the window to the supplied bounds.
Returns Size
- Contains the window's width and height.
Resizes the window to width
and height
.
Returns Offset
- Contains the window's current position.
Moves window to position.
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.
Returns bool
- Whether the window can be manually maximized by the user.
Sets whether the window can be manually maximized by the 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 bool
- Whether the window is always below other windows.
Sets whether the window should show always below 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.
Returns bool
- Whether skipping taskbar is enabled.
Makes the window not show in the taskbar / dock.
Sets progress value in progress bar. Valid range is [0, 1.0].
Sets window/taskbar icon.
Returns bool
- Whether the window is visible on all workspaces.
Sets whether the window should be visible on all workspaces.
Note: If you need to support dragging a window on top of a fullscreen window on another screen, you need to modify MainFlutterWindow to inherit from NSPanel
class MainFlutterWindow: NSPanel {
// ...
}
Set/unset label on taskbar(dock) app icon
Note that it's required to request access at your AppDelegate.swift like this: UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge])
Returns bool
- Whether the window has a shadow. On Windows, always returns true unless window is frameless.
Sets whether the window should have a shadow. On Windows, doesn't do anything unless window is frameless.
Returns double
- between 0.0 (fully transparent) and 1.0 (fully opaque).
Sets the opacity of the window.
Sets the brightness of the window.
Makes the window ignore all mouse events.
All mouse events happened in this window will be passed to the window below this window, but if this window has focus, it will still receive keyboard events.
Starts a window drag based on the specified mouse-down event.
Starts a window resize based on the specified mouse-down & mouse-move event.
Grabs the keyboard.
Ungrabs the keyboard.
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 once when the window has finished being resized.
Emitted when the window is being moved to a new position.
Emitted once when the window is moved to a new position.
Emitted when the window enters a full-screen state.
Emitted when the window leaves a full-screen state.
Emitted when the window entered a docked state.
Emitted when the window leaves a docked state.
Emitted all events.