Flutter 桌面 - 从 Dart 代码更改窗口标题

use*_*216 14 desktop-application dart flutter

在搜索官方文档时我无法找到任何方法来做到这一点。有人知道有什么方法可以做到这一点吗?

tom*_*ozb 20

这是可能的,正如https://github.com/flutter/flutter/issues/30712中提到的

pubspec.yaml添加

dependencies:
  window_size:
    git:
      url: https://github.com/google/flutter-desktop-embedding.git
      path: plugins/window_size
      ref: fd519be1e8398c6b6c2062c2447bac960a71bc02
Run Code Online (Sandbox Code Playgroud)

现在在你的代码中只需调用

import 'package:window_size/window_size.dart';

WidgetsFlutterBinding.ensureInitialized();
setWindowTitle("Custom window title");
Run Code Online (Sandbox Code Playgroud)

在 Linux 上运行良好。尚未在 Windows/Mac 上测试

PS 感谢@Johan Ordenes Galleguillos 为失踪者提供帮助WidgetsFlutterBinding.ensureInitialized();

  • 我必须使用 URL `https://github.com/google/flutter-desktop-embedding.git` 才能使其正常工作,这就是上面提到的问题所使用的。另一方面,这个答案中的网址给了我“pub get failed(服务器不可用)”和无限重试。 (6认同)
  • 无法编辑答案,但请确保添加 WidgetsFlutterBinding.ensureInitialized(); 在 setWindowsTitle("") 之前。 (4认同)

use*_*216 1

好吧,这似乎还不可能,但工作正在进行中:https://github.com/flutter/flutter/issues/30712