Search code examples
windowsflutterdart

Set min/max screen size in Flutter windows


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?


Solution

  • 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: https://github.com/google/flutter-desktop-embedding
          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:

    import 'package:window_size/window_size.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());
    }
    

    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.