flutter桌面软件开发中如何使用window_manager自定义桌面窗口
window_manager 这个插件允许 Flutter 桌面应用调整窗口的大小和位置 ,相比bitsdojo_window它的功能更强大,window_manager 不仅具有bitsdojo_window的功能,还可以监听事件,下面我们一起看看flutter桌面软件开发中如何使用window_manager自定义桌面窗口。
1、安装依赖:
dependencies:
window_manager: ^0.3.2
2、配置窗口大小
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());
}
3、监听事件
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
}
}
Flutter仿小米商城项目实战地址:https://www.itying.com/goods-1176.html
Flutter+Go_Router+Fluent_Ui仿阿里网盘桌面软件开发跨平台实战:https://www.itying.com/goods-1183.html