32

What I'm trying to do is build a flutter web app that, when displayed in the browser, the tab shows an icon and a title (as currently it only shows the world icon and the localhost... title).

Actual Result :

Actual Result

Desired Result :

Desired Result

Edit: I can now add the title, as this is set in the main function

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      leading: Icon(Icons.menu, color: Colors.white,),
      title: Text(_your title here_),
    )
    ...
  );
}

So, the only thing I need to edit is the favicon

Community
  • 1
  • 1
  • 3
    appbar is not for the favicon and title beacause appbar in screen widget so you have to find out that widget in flutter – Hardik Kumbhani Dec 07 '19 at 04:14
  • 1
    If you want to change title dynamically [check this answer](https://stackoverflow.com/questions/60456687/how-to-dynamically-change-the-app-title-in-flutter-web/60456688#60456688) – Adam Smaka Jun 30 '21 at 06:47

5 Answers5

53

In order to change the title to what you desire, you need to add the parameter title (Which is a String) to your MaterialApp widget.

return MaterialApp(
  title: "MyTitle", 
  home: MyHomeWidget());
22

Assuming you already have a favicon.ico file, placing it inside the your_project_dir/web folder alongside index.html as shown below is enough for the browser to pick it up.

enter image description here

Following is the result of placing a favicon.ico in the web folder. Make sure to do a clean build.

enter image description here

In other case you can manually mention it using the link tag inside your index.html as explained here in this Wikipedia page.

ynn
  • 3,386
  • 2
  • 19
  • 42
Abhilash Chandran
  • 6,803
  • 3
  • 32
  • 50
  • 2
    Is there any way to dynamically set the link and the title of the index.html page? When opening a website built with Flutter, I need to get the business's details from my API, and set the favicon to be the business's logo & the title to be the name of the business. – baselsader Oct 13 '20 at 13:55
15
  • Edit title

it is the simplest. just use the Title widget on each page or directly inside the materialApp constructor and set title string key to the title text you need. like this:

...
Title(
  color: myColors, //not  important  in web but still required
  title: 'web  page title',
  child: myChildWidget,
),
...
  • Edit icon

If your app is only for the web, use the dart:html library to perform change using DOM access. something like this

import 'dart:html';
...
...
updateIcon(String assetIcon){
      LinkElement link = (document.querySelector("link[rel*='icon']") ??
          document.createElement('link')) as LinkElement;
      link.type = 'image/x-icon';
      link.rel = 'shortcut icon';
      link.href = assetIcon;
}

if your application is multi-platform, you need to create separate main file for the web like main_web.dart. and declare the previous function inside this file. Now, anywhere you need to set up the icon you just need to call the method after checking the platform using the keyword kIsWeb.

Ex: change icon inside page

...
initState(){
    super.initSate();
    if(kIsWeb){
      WebMixin.updateIcon("assets/home_icon.png"); //WebMixin is just a helper.  replace  it by your  one.
    }
}
...
Taur
  • 514
  • 3
  • 8
12

You could set the onGenerateTitle property of your MaterialApp widget, and provide a callback function to build your title. The onGenerateTitle callback is called each time the WidgetsApp rebuilds. This is useful if you want the title of your page to change dynamically or if you want to produce a localized title.

MaterialApp(
  ...
  onGenerateTitle: (BuildContext context) {
    return AppLocalizations.of(context).myTitle;
  }
  ...
);
SteveM
  • 722
  • 1
  • 6
  • 12
  • can you please tell me about AppLocatizations what it is? – Adnan haider Sep 19 '22 at 09:41
  • https://docs.flutter.dev/development/accessibility-and-localization/internationalization You would need to configure your application to have localized pieces of text by doing something like this. – SteveM Oct 05 '22 at 20:24
8

If you're wondering how to change the app name on your device's homepage, you can update the "name" and "short_name" values in web/manifest.json:

"name": "Ideasky",
"short_name": "Ideasky",
Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61