Set min/max screen size in Flutter windows
Yes, this can be done and it also avoids the problem mentioned by @Khamidjon Khamidov. To achieve this, you will need to make changes in 3 files:
- pubspec.yaml
- main.cpp (at windows\runner\main.cpp)
- main.dart
Add the code below to your pubspec.yaml, under dependencies and save the file.
window_size:
git:
url: git://github.com/google/flutter-desktop-embedding.git
path: plugins/window_size
Change the code below in main.cpp:
Win32Window::Size size(1280, 720);
to
Win32Window::Size size(min_width, min_height)
The above code will ensure your app startup at the preferred size. Replace min_width and min_height with either your minimum or maximum value pair.
Modify your main.dart as show below:
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());
}
Replace max_width, max_height, min_width and min_height with your preferred value.
Note: If you want a non-sizable form use the same min and max values.
Khamidjon Khamidov
Loves Coding. Problem Solver, Android and Flutter Developer, Linux enthusiast
Updated on January 01, 2023Comments
-
Khamidjon Khamidov 10 months
I am developing a Windows flutter application. The app has already mobile versions and I am converting to windows. The mobile design is already there but I need to convert it to windows and the problem is the design is not ready yet.
For this reason, I have been given a task to find out how to give maximum width and height for the application so that the user cannot change the app screen size using the mouse.
Is there a way to implement this on Flutter windows?
-
Khamidjon Khamidov about 2 yearsBut, initially the screen is becoming larger(not becoming what I set as min/max). It is changing only after I try to resize
-
KAnggara75 over 1 yearUpdate: since March 15, 2022 git:// protocol on port 9418 is no longer supported. you can use https:// protocol to github.com/google/flutter-desktop-embedding.git