记录一下,关于flutter桌面版的固定尺寸
一、选择对的插件
如果只是单纯做窗口尺寸限制大小、文件上传等相关设置,那么最好的插件推荐就是这个flutter-desktop-embedding
,该库没有发布到pub上面,如果要引用,如下:
1
2
3
4
5
window_size:
git:
url: git://github.com/google/flutter-desktop-embedding.git
path: plugins/window_size
ref: f2d8aa3820fb87316516670bf4d51a74de8ac0dd
需要注意的是:如果是窗口尺寸,那么path
如上填写没问题,但是如果是要设置选择文件,那么path就要改另外的一个路径,还有一点就是ref
最好改成最近的一次commit
。 如果是要做到自定义窗口,那么就要用到另外一个插件bitsdojo_window
,这个库可以实现完全自定义窗口那种情况,达到的效果如下图所示:
二、上代码教程
说再多也不如直接上代码来的实在,ok,先说flutter-desktop-embedding
这个组件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:window_size/window_size.dart' as window_size;
import 'package:window_size/window_size.dart';
class WindowSizeService {
static const double width = 500;
static const double height = 700;
Future<PlatformWindow> _getPlatformWindow() async {
return await window_size.getWindowInfo();
}
void _setWindowSize(PlatformWindow platformWindow) {
final Rect frame = Rect.fromCenter(
center: Offset(
platformWindow.frame.center.dx,
platformWindow.frame.center.dy,
),
width: width,
height: height,
);
window_size.setWindowFrame(frame);
setWindowTitle(
'${Platform.operatingSystem} App',
);
/// 此处的判断是指,只要是苹果或者微软,那么设置其最大尺寸和最小尺寸, 可以另作调整
if (Platform.isMacOS || Platform.isWindows) {
window_size.setWindowMinSize(Size(width, height));
window_size.setWindowMaxSize(Size(width , height ));
}
}
Future<void> initialize() async {
PlatformWindow platformWindow = await _getPlatformWindow();
if (platformWindow.screen != null) {
if (platformWindow.screen.visibleFrame.width != 800 ||
platformWindow.screen.visibleFrame.height != 500) {
_setWindowSize(platformWindow);
}
}
}
void setWindowTitle(String title) {
window_size.setWindowTitle(title);
}
}
上面先写个配置文件,然后在主方法中吊用一下即可:
1
2
3
4
5
6
7
8
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
final WindowSizeService windowSizeService = WindowSizeService();
windowSizeService.initialize();
}
runApp(new App());
}
完事儿。至于另外一个自定义窗口的插件,可以去看官方的DEMO,也可以看看这个视频