Set min/max screen size in Flutter windows

3,528

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:

  1. pubspec.yaml
  2. main.cpp (at windows\runner\main.cpp)
  3. 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.

Share:
3,528
Khamidjon Khamidov
Author by

Khamidjon Khamidov

Loves Coding. Problem Solver, Android and Flutter Developer, Linux enthusiast

Updated on January 01, 2023

Comments

  • Khamidjon Khamidov
    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
    Khamidjon Khamidov about 2 years
    But, initially the screen is becoming larger(not becoming what I set as min/max). It is changing only after I try to resize
  • KAnggara75
    KAnggara75 over 1 year
    Update: 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