4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutter for Desktopで起動時のウインドウサイズを変える

Last updated at Posted at 2019-12-08

自分用メモ。

2019年12月現在、macOS向けにしか対応してないようなのですが、 window_size(公式)を使うとデスクトップアプリの起動時のウインドウサイズを変更することができるようです。
https://github.com/google/flutter-desktop-embedding/pull/362

pubspec.yaml
dependencies:
  flutter:
    sdk: flutter

  # ↓をdependenciesに追加
  window_size:
    git:
      url: git://github.com/google/flutter-desktop-embedding
      path: plugins/window_size
lib/main.dart
import 'package:flutter/material.dart';
import 'package:window_size/window_size.dart';

// Sets a platform override for desktop to avoid exceptions. See
// https://flutter.dev/desktop#target-platform-override for more info.
void _enablePlatformOverrideForDesktop() {
  if (!kIsWeb && (Platform.isMacOS || Platform.isWindows || Platform.isLinux)) {
    debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
  }
}

void main() {
  _enablePlatformOverrideForDesktop();
  WidgetsFlutterBinding.ensureInitialized(); // ←これがないと初期化前に実行するな的なエラーが出る
  if (Platform.isMacOS) {
    getWindowInfo().then((windowInfo) { // OSによって配置された場所・サイズを取得し
      setWindowFrame( // 場所はそのままに、widthとheightを変更
        Rect.fromCenter(
          center: windowInfo.frame.center,
          width: 480,
          height: 640,
        )
      );
    });
  }
  runApp(MyApp());
}

https://github.com/google/flutter-desktop-embedding/blob/master/plugins/README.md
にも注意書きがあるように、あくまで "early stage desktop plugins" なので、自己責任で。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?