Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default size of macos app in flutter?

I am trying to build a macOS desktop app with flutter. I want the app to be full-width, edge-to-edge. However, when I run the app via the simulator, or after the build, it always launches the app with size 800x600.

I have set the height and width of the root container to double.infinity. In fact, even if I set the height and width to 10.0, it always launches the app with 800x600. I am new to flutter, so probably missing some fundamentals. Most tutorials I have come across talk about building a mobile app where this is never a problem because the app always launches to its full width.

Here is my entire test app code:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: Colors.white),
      height: double.infinity,
      width: double.infinity,
      child: Center(
        child: Text(
          'Hello World',
          textDirection: TextDirection.ltr,
          style: TextStyle(
              fontSize: 32, fontWeight: FontWeight.bold, color: Colors.black),
        ),
      ),
    );
  }
}

like image 331
deltavin Avatar asked Apr 27 '20 03:04

deltavin


People also ask

How do I fix the window size in Flutter?

You can set the window size inside the main() function before runApp() is called. In the above code snippet, the window size is set to have a minimum screen width of 400 pixels and a minimum screen height of 300 pixels . If you try to squeeze the Flutter app window now, it won't shrink beyond the minimum size.

Why does the first Flutter app build take so long?

Why does the first Flutter app build take so long? When building a Flutter app for the first time, a device-specific APK or IPA file is built. Hence, Gradle and XCode are used to build the files, taking time.


1 Answers

There's now a plugin to do this, which is not a permanent thing as it is described as preliminary functionality before eventually being folded into the core libraries.

Using the plugin for now is still likely to be better than hard-coding directly modifying the native code, especially if you have multiple platforms you want to work on.

First add to the pubspec.yaml something like:

dependencies:
  ...
  window_size:
    git:
      url: git://github.com/google/flutter-desktop-embedding.git
      path: plugins/window_size
      ref: 927f8cbc09b35d85245c095f2db8df9b186f6618

Using the specific Git reference to include this, as shown above, will give you good control over when you choose to pull updated code and make any changes this might entail.

You can then access various functions to set min/max window sizes, or frame, or get the current values, e.g.:

...
import 'dart:io'
import 'package:window_size/window_size.dart';
...
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
    setWindowTitle("My Desktop App");
    setWindowMinSize(Size(375, 750));
    setWindowMaxSize(Size(600, 1000));
  }
  runApp(MyApp());
}

I hope this helps someone. I'll try and update this post when the real answer comes out. It seems likely that the interface will approximate what is presented in this library, but the feature set is likely to undergo some change.

like image 133
karora Avatar answered Oct 07 '22 19:10

karora