30.15 Flutter Desktop App Window Title

20220315

The flutter native desktop app will have a default window title the same as the app name. To change it, we can utilise the window_size package.

First add the dependency to pubspec.yaml:

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

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 lines around the setWindowTitle():

import 'package:flutter/foundation.dart';
import 'package:window_size/window_size.dart';

...

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    
  if (defaultTargetPlatform == TargetPlatform.linux || 
      defaultTargetPlatform == TargetPlatform.macOS || 
      defaultTargetPlatform == TargetPlatform.windows) {
    setWindowTitle("Community POD Demo");
  }
  
    return MaterialApp(
    
    ...

An older approach (pre March 2022) was:

import 'dart:io';
import 'package:window_size/window_size.dart';

...

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    
    try {
      if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
        setWindowTitle("Community POD Demo");
      }
    } catch(e) {}; \\ Ignore.
  
    return MaterialApp(
    
    ...

Notice wrapping the test for Platform within a try to avoid an exception being raised with the web version of the app.

Also see https://www.kindacode.com/article/flutter-web-2-ways-to-change-page-title-dynamically/.



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