Flutter桌面窗口管理插件desktop_window_utils的使用
Flutter桌面窗口管理插件desktop_window_utils的使用
一个用于控制窗口及其属性和工具栏/标题栏的插件。

如何使用它?
首先,你需要将该依赖添加到你的项目中。请注意,目前实现仅适用于macOS。
添加依赖
在pubspec.yaml
文件中添加以下依赖:
dependencies:
desktop_window_utils: ^0.0.3
然后,在你的Dart文件中导入该库:
import 'package:desktop_window_utils/desktop_window_utils.dart';
功能
目前支持的功能有:
设置最小窗口大小
你可以定义窗口的最小尺寸。
// 设置窗口的最小高度为300像素,宽度为300像素。
DesktopWindowUtils.setMinimumSize(height: 300, width: 300);
程序化设置窗口大小
你可以程序化地设置窗口的尺寸。
// 将窗口大小设置为高400像素,宽400像素。
DesktopWindowUtils.setFrameSize(height: 400, width: 400);
关闭当前窗口
你可以程序化地关闭当前窗口。
// 关闭当前窗口。
DesktopWindowUtils.closeWindow();
由于macOS的限制,你可能无法将窗口恢复到前台。你应该在macos/Runner/AppDelegate.swift
文件中添加以下代码来处理窗口操作。
override func applicationShouldHandleReopen(_: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag {
for window: AnyObject in NSApplication.shared.windows {
window.makeKeyAndOrderFront(self)
}
}
return true
}
最小化当前窗口
你可以程序化地最小化当前窗口。
// 最小化当前窗口。
DesktopWindowUtils.minimizeWindow();
由于macOS的限制,你可能无法将窗口恢复到前台。你应该在macos/Runner/AppDelegate.swift
文件中添加以下代码来处理窗口操作。
override func applicationShouldHandleReopen(_: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag {
for window: AnyObject in NSApplication.shared.windows {
window.makeKeyAndOrderFront(self)
}
}
return true
}
打开/恢复当前窗口
你可以程序化地打开或恢复当前窗口,无论其状态(最小化或关闭)如何。
// 打开或恢复当前窗口。
DesktopWindowUtils.openWindow();
使用工具栏代替标题栏
你可以启用工具栏视图代替macOS应用的默认标题栏。
// 启用工具栏。
DesktopWindowUtils.useToolbar(isUsingToolbar: true);
移除工具栏/标题栏分隔线
你可以移除工具栏或标题栏的分隔线(根据你正在使用的组件)。
// 隐藏分隔线并启用工具栏。
DesktopWindowUtils.setTopbarSpecifications(
isDividerInvisible: true,
isUsingToolbar: true,
);
即将推出的功能
- ❌ 透明窗口
- ❌ 透明工具栏/标题栏
- ❌ 自定义背景颜色的工具栏/标题栏
许可证
Window Utils 是在Apache License, Version 2.0下发布的。完整的许可证文本可以在这里查看。
示例代码
以下是使用desktop_window_utils
插件的完整示例代码:
import 'package:flutter/material.dart';
import 'package:desktop_window_utils/desktop_window_utils.dart';
void main() {
runApp(WindowUtilsShowcaseApp());
}
class WindowUtilsShowcaseApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.white54,
body: WindowUtilsShowcaseButtonsPage(),
),
);
}
}
class WindowUtilsShowcaseButtonsPage extends StatefulWidget {
[@override](/user/override)
_WindowUtilsShowcaseButtonsPageState createState() => _WindowUtilsShowcaseButtonsPageState();
}
class _WindowUtilsShowcaseButtonsPageState extends State<WindowUtilsShowcaseButtonsPage> {
[@override](/user/override)
Widget build(BuildContext context) {
return ListView(
children: [
ListTile(
title: Text('使用工具栏'),
onTap: () async {
// 启用工具栏。
DesktopWindowUtils.useToolbar(isUsingToolbar: true);
},
),
ListTile(
title: Text('设置最小屏幕尺寸为300x300'),
onTap: () async {
// 设置窗口的最小高度为300像素,宽度为300像素。
DesktopWindowUtils.setMinimumSize(height: 300, width: 300);
},
),
ListTile(
title: Text('设置屏幕尺寸为400x400'),
onTap: () async {
// 将窗口大小设置为高400像素,宽400像素。
DesktopWindowUtils.setFrameSize(height: 400, width: 400);
},
),
ListTile(
title: Text('关闭窗口'),
onTap: () async {
// 关闭当前窗口。
DesktopWindowUtils.closeWindow();
},
),
ListTile(
title: Text('最小化窗口'),
onTap: () async {
// 最小化当前窗口。
DesktopWindowUtils.minimizeWindow();
},
),
ListTile(
title: Text('移除标题栏分隔线'),
onTap: () async {
// 隐藏分隔线。
DesktopWindowUtils.setTopbarSpecifications(isDividerInvisible: true);
},
),
ListTile(
title: Text('移除工具栏分隔线'),
onTap: () async {
// 隐藏分隔线并启用工具栏。
DesktopWindowUtils.setTopbarSpecifications(
isDividerInvisible: true,
isUsingToolbar: true,
);
},
),
],
);
}
}
更多关于Flutter桌面窗口管理插件desktop_window_utils的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复