flutter:更换图标icons_launcher(不支持删除)

  1. 安装插件
  2. 配置图标
  3. 生成图标
  • 输出名是固定的
  • 不支持删除(差评)
  • icons_launcher 是一个 Flutter 的命令行工具,用于简化更新应用启动图标的任务。它允许开发者为不同平台(如 Android、iOS、Web、macOS、Windows 和 Linux)生成所需的启动图标。

    安装插件

    flutter pub add icons_launcher -d
    

    配置图标

    pubspec.yaml 或单独的配置文件(如 icons_launcher.yaml)中添加图标配置:

    icons_launcher:
      image_path: "assets/ic_logo_radius.png"
      platforms:
        android:
          enable: true
          notification_image: "assets/icons/ic_notification.png"
          adaptive_background_image: "assets/ic_background.png"
          adaptive_foreground_image: "assets/ic_foreground.png"
        ios:
          enable: true
          dark_path: "assets/ic_dark.png"
          tinted_path: "assets/ic_tinted.png"
        web:
          enable: true
          favicon_path: "assets/ic_favicon.png"
          sizes: [16, 32, 48, 64, 128, 256]
    
    • image_path:主图标源文件路径,工具会基于它生成多分辨率图标。建议至少 1024x1024 px。路径是在项目的根目录下面:项目个目录/assets/ic_logo_radius.png
    • platforms:每个平台单独配置,控制是否生成图标及平台特有选项。
    • enable:是否生成 Android/ios/ web/…图标。
    • adaptive_background_image:自适应图标背景图片(Android 8.0+ 支持)。
    • adaptive_foreground_image:自适应图标前景图片。
    • notification_image:生成 Android 推送通知的小图标,通常显示在状态栏。图标背景最好是透明,纯白色或单色线条,避免彩色图标在通知栏显示不清。
    • dark_path:iOS 暗黑模式图标。
    • inted_path:iOS 自动着色图标。
    • 不配置 dark_pathtinted_path 时,iOS 会使用 image_path 生成标准图标。
    • favicon_path:favicon 源图标。
    • sizes(可选):favicon 生成的尺寸列表,默认常用 16x16、32x32、48x48。

    生成图标

    配置完成后,运行以下命令生成图标:

    flutter pub get
    dart run icons_launcher:create
    # 或者:flutter pub run icons_launcher:create
    

    这个命令会做哪些具体修改,按平台和文件整理如下:

    平台 修改内容 文件/目录
    Android 自适应图标、普通图标 android/app/src/main/res/mipmap-*/
    iOS AppIcon 资源 ios/Runner/Assets.xcassets/AppIcon.appiconset/
    Web favicon web/icons/web/favicon.ico
    macOS/Windows/Linux 平台启动图标 各自资源目录

    不在是flutter的默认图标。

    输出名是固定的

    它的工作方式是:在你执行dart run icons_launcher:create 之后,会根据配置文件(pubspec.yaml 里的 icons_launcher 部分)生成应用图标,替换掉 iOS 和 Android 工程目录下的相关资源文件。

    icons_launcher:
      image_path: "assets/icons/ic_logo.png"
      platforms:
        android:
          enable: true
          notification_image: "assets/icons/ic_foreground.png"
          # adaptive_background_color: "#ffffff"
          adaptive_background_image: "assets/icons/ic_background.png"
          adaptive_foreground_image: "assets/icons/ic_foreground.png"
        ios:
          enable: true
    

    上面的 ic_logo.pngic_foreground.pngic_background.png 完全可以随便取名字,比如 abc.pngmy_icon.png,只要路径正确、能读到文件就行。这些只是 输入源文件

    最终生成到 Android / iOS 工程里的文件是固定的:

    // Android
    res/mipmap-mdpi/ic_launcher.png
    res/mipmap-hdpi/ic_launcher.png
    ...
    res/mipmap-xxxhdpi/ic_launcher.png
    res/mipmap-*/ic_launcher_round.png
    res/mipmap-anydpi-v26/ic_launcher.xml
    
    // iOS
    AppIcon.appiconset/Icon-App-20x20@2x.png
    AppIcon.appiconset/Icon-App-60x60@3x.png
    ...
    AppIcon.appiconset/Contents.json
    

    不支持删除(差评)

    icons_launcher 这个包本身并没有提供一个单独的 删除命令

    所以删除图标实际上就是恢复到默认图标。常见做法有两种:

    方法 1:手动恢复

    • iOS:删除 ios/Runner/Assets.xcassets/AppIcon.appiconset 下的生成图标,并用 Xcode 默认的 AppIcon.appiconset 替换回来。
    • Android:删除 android/app/src/main/res/mipmap-*/ic_launcher.png(以及 ic_launcher_round.png),改回默认的 Flutter 图标(或者 Flutter 创建新项目拷贝过来)。

    方法2:替换现有图标

    • 可以直接创建一个新的 Flutter 工程。
    • 然后把新项目里面的默认图标资源拷贝到现有项目里
    • 接着执行dart run icons_launcher:create ,覆盖掉被 icons_launcher 修改过的文件。

    ×

    喜欢就点赞,疼爱就打赏