我正在开发一个Windows Flutter应用程序。该应用程序已经有移动的版本,我正在转换到Windows。移动设计已经存在,但我需要将其转换到Windows,问题是设计还没有准备好。出于这个原因,我已经被赋予了一个任务,找出如何给出应用程序的最大宽度和高度,使用户无法使用鼠标更改应用程序屏幕大小。有没有办法在Flutter窗口上实现这一点?
busg9geu1#
是的,这是可以做到的,它也避免了@Khamidjon Khamidov提到的问题。要实现这一点,你需要在3个文件中进行更改:
将下面的代码添加到pubspec.yaml的依赖项下并保存文件。
window_size: git: url: https://github.com/google/flutter-desktop-embedding path: plugins/window_size
更改main.cpp中的以下代码:
Win32Window::Size size(1280, 720); to Win32Window::Size size(min_width, min_height)
以上代码将确保您的应用以首选大小启动。请将min_width和min_height替换为最小值或最大值对。
修改您的main.dart,如下所示:
void main() { WidgetsFlutterBinding.ensureInitialized(); if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) { setWindowTitle('My App'); setWindowMaxSize(const Size(max_width, max_height)); setWindowMinSize(const Size(min_width, min_height)); } runApp(const MyApp()); }
用首选值替换max_width、max_height、min_width和min_height。注意:如果您想要不可调整大小的表单,请使用相同的最小值和最大值。
i2byvkas2#
METHOD 1正如@Stefano提到的,我可以使用这个库:
METHOD 1
dependencies: window_size: git: url: git://github.com/google/flutter-desktop-embedding.git path: plugins/window_size
。
import 'dart:io'; import 'package:flutter/material.dart'; import 'package:window_size/window_size.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); if (Platform.isWindows) { setWindowMaxSize(const Size(1024, 768)); setWindowMinSize(const Size(512, 384)); Future<Null>.delayed(Duration(seconds: 1), () { setWindowFrame(Rect.fromCenter(center: Offset(1000, 500), width: 600, height: 1000)); }); } runApp(MyApp()); }
但这里的问题是窗口大小被设置为默认值,所以它会在几秒钟后更改其大小。METHOD 2因为,第一种方法并没有像我预料的那样奏效我把这两种方法都混合了:在main.dart中:
METHOD 2
void main() { WidgetsFlutterBinding.ensureInitialized(); if (Platform.isWindows) { setWindowMaxSize(const Size(1024, 768)); setWindowMinSize(const Size(512, 384)); } runApp(MyApp()); }
在库/窗口/运行程序/win32_window.cpp中://这个方法已经存在于文件bool Win32 Window::CreateAndShow(const std::wstring& title,const Point& origin,const Size& size){ Destroy();
const wchar_t* window_class = WindowClassRegistrar::GetInstance()->GetWindowClass(); const POINT target_point = {static_cast<LONG>(/*x // change here to move to center*/ 550), // before -> origin.x static_cast<LONG>(/*y // change here to move vertically*/ origin.y)}; // before -> origin.y HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST); UINT dpi = FlutterDesktopGetDpiForMonitor(monitor); double scale_factor = dpi / 96.0; HWND window = CreateWindow( window_class, title.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE, Scale(/* x // move to center */ 550, scale_factor), Scale(/* y // move screen vertically */ origin.y, scale_factor),// before -> origin.x, origin.y Scale(/* width // set default width */ 450, scale_factor), Scale(/* height // set default height */ 800, scale_factor), // before -> size.width, size.height nullptr, nullptr, GetModuleHandle(nullptr), this); if (!window) { return false; } return OnCreate(); }
i86rm4rw3#
是的,我们可以使用window_size包来限制Windows Flutter应用的大小。要在我们的应用中使用它,我们需要将其添加到pubspec.yaml依赖项中:
window_size
pubspec.yaml
dependencies: flutter: sdk: flutter window_size: git: url: git://github.com/google/flutter-desktop-embedding.git path: plugins/window_size
并在初始化时使用,如下所示:
import 'dart:io'; import 'package:flutter/material.dart'; import 'package:window_size/window_size.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); if (Platform.isWindows) { setWindowMaxSize(const Size(1024, 768)); setWindowMinSize(const Size(512, 384)); } runApp(MyApp()); }
kiz8lqtg4#
您可以使用此Plugin来添加控制窗口示例的功能。
安装
将此添加到您的pubspec.yaml:
dependencies: window_manager: ^0.2.7
用法
在main()方法中添加以下内容:
main()
void main() async{ WidgetsFlutterBinding.ensureInitialized(); await windowManager.ensureInitialized(); if (Platform.isWindows) { WindowManager.instance.setMinimumSize(const Size(1200, 600)); WindowManager.instance.setMaximumSize(const Size(1200, 600)); } runApp(MyApp()); }
并将1200和600替换为您的首选尺寸。
pftdvrlh5#
您可以使用像这样的方法,而不需要额外的套件。这是设定最小 windows 大小的范例:"窗口大小“,{”宽度“:500,“身高”:800);
5条答案
按热度按时间busg9geu1#
是的,这是可以做到的,它也避免了@Khamidjon Khamidov提到的问题。要实现这一点,你需要在3个文件中进行更改:
将下面的代码添加到pubspec.yaml的依赖项下并保存文件。
更改main.cpp中的以下代码:
以上代码将确保您的应用以首选大小启动。请将min_width和min_height替换为最小值或最大值对。
修改您的main.dart,如下所示:
用首选值替换max_width、max_height、min_width和min_height。
注意:如果您想要不可调整大小的表单,请使用相同的最小值和最大值。
i2byvkas2#
METHOD 1
正如@Stefano提到的,我可以使用这个库:
。
但这里的问题是窗口大小被设置为默认值,所以它会在几秒钟后更改其大小。
METHOD 2
因为,第一种方法并没有像我预料的那样奏效我把这两种方法都混合了:
在main.dart中:
在库/窗口/运行程序/win32_window.cpp中://这个方法已经存在于文件bool Win32 Window::CreateAndShow(const std::wstring& title,const Point& origin,const Size& size){ Destroy();
i86rm4rw3#
是的,我们可以使用
window_size
包来限制Windows Flutter应用的大小。要在我们的应用中使用它,我们需要将其添加到
pubspec.yaml
依赖项中:并在初始化时使用,如下所示:
kiz8lqtg4#
您可以使用此Plugin来添加控制窗口示例的功能。
安装
将此添加到您的
pubspec.yaml
:用法
在
main()
方法中添加以下内容:并将1200和600替换为您的首选尺寸。
pftdvrlh5#
您可以使用像这样的方法,而不需要额外的套件。这是设定最小 windows 大小的范例:
"窗口大小“,{”宽度“:500,“身高”:800);