如何为我的 Flutter Web 应用程序配置图标?

pol*_*a-c 28 flutter

我正在尝试使用 flutter for web,但找不到如何配置图标的信息。

这应该是可能的,因为taphero_web以某种方式做到了。我查看了他们的来源,但找不到他们为该网站提供的图片:

在此处输入图片说明

我使用 codemagic.io 托管我的应用程序。有任何想法吗?

Mar*_*lla 44

Glad you ask! As the owner of that project I can tell you how I did it:

UPDATE: With PWA support and Flutter 2

1- Inside your /web folder (to be pushed to the server), add a /icons folder (if your don't have it already).

2- Once you're there, you need to upload your set of images with websites like this one.

3- Is going to look something like this:

图标文件夹

4- You should have a manifest.json and is going to look something like this:

{
    "name": "Tap Hero",
    "short_name": "Tap Hero",
    "start_url": ".",
    "display": "standalone",
    "background_color": "#000000", // any HEX color works
    "theme_color": "#000000", // any HEX color works
    "description": "anything you want here",
    "orientation": "portrait" // or changed as you wish
    "prefer_related_application": false,
    "icons": [
        {
            "src": "\/icons/android-icon-36x36.png",
            "sizes": "36x36",
            "type": "image\/png",
            "density": "0.75"
        },
        {
            "src": "\/icons/android-icon-48x48.png",
            "sizes": "48x48",
            "type": "image\/png",
            "density": "1.0"
        },
        {
            "src": "\/icons/android-icon-72x72.png",
            "sizes": "72x72",
            "type": "image\/png",
            "density": "1.5"
        },
        {
            "src": "\/icons/android-icon-96x96.png",
            "sizes": "96x96",
            "type": "image\/png",
            "density": "2.0"
        },
        {
            "src": "\/icons/android-icon-144x144.png",
            "sizes": "144x144",
            "type": "image\/png",
            "density": "3.0"
        },
        {
            "src": "\/icons/android-icon-192x192.png",
            "sizes": "192x192",
            "type": "image\/png",
            "density": "4.0"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

5- Once you did all that, add the rest of the icons inside your index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Tap Hero</title>
    <meta name="description" content="Tap Hero">
    <meta name="keywords" content="Flutter, Tap, Hero, Game">
    <meta name="author" content="Mariano Zorrilla">
    <meta name="theme-color" content="#6200EA" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta content="IE=Edge" http-equiv="X-UA-Compatible">
    <link rel="apple-touch-icon" sizes="57x57" href="icons/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="icons/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="icons/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="icons/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="icons/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="icons/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="icons/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="icons/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="icons/apple-icon-180x180.png">
    <link rel="icon" type="image/png" sizes="192x192" href="icons/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="icons/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="96x96" href="icons/favicon-96x96.png">
    <link rel="icon" type="image/png" sizes="16x16" href="icons/favicon-16x16.png">

    <!-- Main Favicon -->
    <link rel="icon" type="image/pg" href="favicon/png"/>

    <!-- ALTERNATIVE <link rel="icon" type="image/x-icon" href="favicon.ico" /> -->
    
    <link rel="manifest" href="icons/manifest.json">
    <meta name="msapplication-TileColor" content="#6200EA">
    <meta name="msapplication-TileImage" content="icons/ms-icon-144x144.png">
    <script>
       if ('serviceWorker' in navigator) {
         window.addEventListener('flutter-first-frame', function () {
           navigator.serviceWorker.register('flutter_service_worker.js');
         });
       }
    </script>
    <script defer src="main.dart.js" type="application/javascript"></script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
  1. IF (by any chance) your /web and or manifest.json files are corrupted/broken/etc you can delete the entire file and do "flutter create ." that will generate everything again for you and can do a new try every single time.

  • 是的,我使用了我在此处发布的资产链接(“这个”):https://www.favicon-generator.org/“要推送到服务器的文件夹”是保存您的 index.html 的文件夹,等在执行 webdev 构建后生成的 (3认同)

小智 16

我开始按照 Mariano Zorilla 的回答中的说明进行操作,但发现如果您只想为 Flutter Web App 设置 Favicon,它会简单得多。

  1. 像 Mariano 建议的那样的图标创建网站中上传您的图像。
  1. 选择“仅生成 16x16 favicon.ico”选项并下载图标文件。

  2. web/文件夹中,用favicon.png新生成的favicon.ico文件替换默认的颤振

  3. web/index.html文件中,

代替:

<link rel="shortcut icon" type="image/png" href="favicon.png"/>
Run Code Online (Sandbox Code Playgroud)

和:

<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
Run Code Online (Sandbox Code Playgroud)

  • 使用此解决方案,“将图标保存到主屏幕”是否有效?或者只是在地址栏上显示一个小图标? (3认同)

Ban*_*eil 8

要为 flutter web 配置您的网站图标,只需将路径中的文件替换为./web/favicon.png您自己的图标,然后运行flutter build web.

  • 只是想强调香蕉说的话。如果 Flutter 应用程序当前正在运行,请确保重建该应用程序。我已经运行了应用程序并进行了更改,但刷新后没有任何反应 (5认同)