Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set icon to flutter desktop linux app?

I've a flutter desktop linux app and I want to set icon app so when I build the app by typing this command flutter build linux the executable file got this icon and this icon appears at the taskbar.

At this moment the executable file has set a generic icon and when the app is running then the taskbar shows an empty icon.

enter image description here

I've read the answer on https://stackoverflow.com/a/73134323/9301998 but this only shows the icon on the taskbar if I run the app typing the command flutter run -d linux but when I build the app by typing this command flutter build linux it's not assign the icon that I want.

The image that I want to set as icon I've added as assets in the pubspec.yaml file.

Then I've created a my_app.desktop file with:

[Desktop Entry]
Version=1.0
Type=Application
Name=my_app
Exec=/home/pepe/my_app/build/linux/x64/release/bundle/my_app
Icon=/home/pepe/my_app/build/linux/x64/release/bundle/data/flutter_assets/assets/images/logo.ico
Comment=Welcome to the flutterverse!
Categories=Development;
Terminal=false
StartupNotify=true;

Then I can see the icon on the desktop, I still add the app as favorite to the taskbar, but when I click on the icon to run the app it runs but it continues adding an empty icon in the taskbar.

This article explains How to change icon of linux desktop app but it's through snapstore and I would like to do this when I build the app by typing this command flutter build linux directly.

like image 844
Jaime Roman Avatar asked Sep 14 '25 21:09

Jaime Roman


1 Answers

I found a workaround to update the generated code in 'linux/my_application.cc' file. Hope this can help you.

// Implements GApplication::activate.
static void my_application_activate(GApplication* application) {
  MyApplication* self = MY_APPLICATION(application);
  GtkWindow* window =
      GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application)));
  // set icon 
  GdkPixbuf* icon = gdk_pixbuf_new_from_file("/home/mingxi/Work/Code/flutter-projects/flutter_application_1/assets/images/flutter_logo.png", NULL);
  if (icon != NULL) {
    gtk_window_set_icon(GTK_WINDOW(window), icon);
    g_object_unref(icon);
  }
  // Use a header bar when running in GNOME as this is the common style used
  // by applications and is the setup most users will be using (e.g. Ubuntu
  // desktop).
  // If running on X and not using GNOME then just use a traditional title bar
  // in case the window manager does more exotic layout, e.g. tiling.
  // If running on Wayland assume the header bar will work (may need changing
  // if future cases occur).
  gboolean use_header_bar = TRUE;

click here for details

like image 188
mingxi gao Avatar answered Sep 17 '25 17:09

mingxi gao