31.44 Flutter Desktop Window Title
20230805
See Section 31.43 to set the window title for a desktop application through the Window Manager.
Alternatively, though no longer recommended, we can set the default window title using the window_size package which is not part of https://pud.dev but can be obtained from https://github.com/google/flutter-desktop-embedding.
Add the following to pubspec.yaml
:
dependencies:
...
window_size:
git:
url: https://github.com/google/flutter-desktop-embedding.git
path: plugins/window_size
ref: 6c66ad23ee79749f30a8eece542cf54eaf157ed8
...
The ref: is the github SHA hash key of the commit to use. Choose the latest one at the time of implementing. It can be dropped but is useful to avoid code suddenly failing.
Then in lib/main.dart
import the required libraries
(foundation.dart
is required for the Platform
checks) and add the
conditional statement with setWindowTitle()
. We do this in main()
so that it is set before the app runs to ensure the app does not
stutter with the original window title in starting up.
import 'package:flutter/foundation.dart';
import 'package:window_size/window_size.dart';
void main() {
// Identify if Desktop or Mobile app.
desktop = [TargetPlatform.linux, TargetPlatform.macOS, TargetPlatform.windows];
bool isDesktop = defaultTargetPlatform in desktop;
// Tune the window manager before runApp to avoid a lag in the UI.
if (isDesktop) {
WidgetsFlutterBinding.ensureInitialized();
setWindowTitle(APP_TITLE);
}
...
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
...
Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0