Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you set title and icon for a flutter web app?

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

like image 402
Bruno Amezcua Arévalo Avatar asked Dec 06 '19 22:12

Bruno Amezcua Arévalo


People also ask

How do I change my Flutter app icon and name?

To change appname, navigate to android\app\src\main\AndroidManifest. xml and change android:label into your app name .

How do you change the icon on Flutter app?

android: and ios: icon/path/here. png : This will generate a new app icon for the platform with the name you specify, without removing the old default existing Flutter launcher icon.


3 Answers

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());
like image 160
Diego Cattarinich Clavel Avatar answered Oct 07 '22 14:10

Diego Cattarinich Clavel


  • 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.
    }
}
...
like image 36
Taur Avatar answered Oct 07 '22 13:10

Taur


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 theweb` 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.

like image 18
Abhilash Chandran Avatar answered Oct 07 '22 14:10

Abhilash Chandran