Flutter 修改應(yīng)用程序的名稱和圖標(biāo)

Android

修改應(yīng)用程序的名稱

在項目中找到 AndroidManifest.xml 文件,其中 android:label="demo" 就是應(yīng)用程序名稱,修改引號中的內(nèi)容即可

android/app/src/main/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.demo.demo">
   <application
        android:label="demo"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        ...
    </application>
</manifest>

修改應(yīng)用程序的圖標(biāo)

在項目中找到 mipmap-mdpi mipmap-hdpi mipmap-xhdpi mipmap-xxhdpi mipmap-xxxhdpi 文件夾,替換這些文件夾中的 ic_launcher.png 文件即可

android/app/src/main/res

?  res tree
.
├── drawable
│   └── launch_background.xml
├── drawable-v21
│   └── launch_background.xml
├── mipmap-mdpi
│   └── ic_launcher.png  # 圖標(biāo)大小為 48x48 像素
├── mipmap-hdpi
│   └── ic_launcher.png  # 圖標(biāo)大小為 72x72 像素
├── mipmap-xhdpi
│   └── ic_launcher.png  # 圖標(biāo)大小為 96x96 像素
├── mipmap-xxhdpi
│   └── ic_launcher.png  # 圖標(biāo)大小為 144x144 像素
├── mipmap-xxxhdpi
│   └── ic_launcher.png  # 圖標(biāo)大小為 192x192 像素
├── values
│   └── styles.xml
└── values-night
    └── styles.xml

9 directories, 9 files

注意:圖標(biāo)有多種尺寸的大小,是為了適配不同分辨率的手機而設(shè)計的

iOS

修改應(yīng)用程序的名稱

在項目中找到 Info.plist 文件,其中 CFBundleDisplayNameCFBundleName 下面的就是應(yīng)用程序名稱,修改內(nèi)容即可

ios/Runner/Info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    ...
    <key>CFBundleDisplayName</key>
    <string>demo</string>
    ...
    <key>CFBundleName</key>
    <string>demo</string>
    ...
</dict>
</plist>

修改應(yīng)用程序的圖標(biāo)

找到項目中的 AppIcon.appiconset 文件夾,其中 Contents.json 是配置文件,其它的圖片文件就是圖標(biāo),替換這些圖片文件即可

ios/Runner/Assets.xcassets/AppIcon.appiconset

?  AppIcon.appiconset tree
.
├── Contents.json
├── Icon-App-20x20@1x.png  # 圖標(biāo)大小為 20x20 像素
├── Icon-App-20x20@2x.png  # 圖標(biāo)大小為 40x40 像素
├── Icon-App-20x20@3x.png  # 圖標(biāo)大小為 60x60 像素
├── Icon-App-29x29@1x.png  # 圖標(biāo)大小為 29x29 像素
├── Icon-App-29x29@2x.png  # 圖標(biāo)大小為 58x58 像素
├── Icon-App-29x29@3x.png  # 圖標(biāo)大小為 87x87 像素
├── Icon-App-40x40@1x.png  # 圖標(biāo)大小為 40x40 像素
├── Icon-App-40x40@2x.png  # 圖標(biāo)大小為 80x80 像素
├── Icon-App-40x40@3x.png  # 圖標(biāo)大小為 120x120 像素
├── Icon-App-60x60@2x.png  # 圖標(biāo)大小為 120x120 像素
├── Icon-App-60x60@3x.png  # 圖標(biāo)大小為 180x180 像素
├── Icon-App-76x76@1x.png  # 圖標(biāo)大小為 76x76 像素
├── Icon-App-76x76@2x.png  # 圖標(biāo)大小為 152x152 像素
├── Icon-App-83.5x83.5@2x.png  # 圖標(biāo)大小為 167x167 像素
└── Icon-App-1024x1024@1x.png  # 圖標(biāo)大小為 1024x1024 像素

0 directories, 16 files

注意:圖標(biāo)有多種尺寸的大小,是為了適配不同分辨率的手機而設(shè)計的

使用插件修改應(yīng)用程序的名稱

插件地址:https://pub.dev/packages/flutter_app_name

安裝插件

在項目中找到 pubspec.yaml 文件,添加內(nèi)容如下

dev_dependencies:
  flutter_test:
    sdk: flutter
  
  # 添加插件
  flutter_app_name: ^0.1.0

# 為插件設(shè)置參數(shù)
flutter_app_name:
  name: "演示程序"

執(zhí)行命令并生成應(yīng)用程序的名稱

# 拉取插件
flutter pub get
# 生成應(yīng)用程序的名稱
flutter pub run flutter_app_name

使用插件修改應(yīng)用程序的圖標(biāo)

插件地址:https://pub.dev/packages/flutter_launcher_icons

安裝插件

在項目中找到 pubspec.yaml 文件,添加內(nèi)容如下

dev_dependencies:
  flutter_test:
    sdk: flutter
  
  # 添加插件
  flutter_launcher_icons: "^0.9.2"

# 為插件設(shè)置參數(shù)
flutter_icons:
  android: "launcher_icon"
  ios: true
  image_path: "assets/icon/icon.png"

注意:準(zhǔn)備一張 1024x1024png 圖片,取名為 icon.png 并把它放在 assets/icon 目錄中

執(zhí)行命令并生成應(yīng)用程序的圖標(biāo)

# 拉取插件
flutter pub get
# 生成應(yīng)用程序的圖標(biāo)
flutter pub run flutter_launcher_icons:main

解決報錯問題

錯誤如下
  ════════════════════════════════════════════
     FLUTTER LAUNCHER ICONS (v0.9.1)                               
  ════════════════════════════════════════════
  

? Successfully generated launcher icons
Unhandled exception:
FormatException: Invalid number (at character 1)

^

#0      int._handleFormatError (dart:core-patch/integers_patch.dart:129:7)
#1      int.parse (dart:core-patch/integers_patch.dart:55:14)
#2      minSdk (package:flutter_launcher_icons/android.dart:309:18)
#3      createIconsFromConfig (package:flutter_launcher_icons/main.dart:94:47)
#4      createIconsFromArguments (package:flutter_launcher_icons/main.dart:60:7)
#5      main (file:///Users/dabolau/snap/flutter/.pub-cache/hosted/pub.flutter-io.cn/flutter_launcher_icons-0.9.2/bin/main.dart:6:26)
#6      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295:32)
#7      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
pub finished with exit code 255
解決辦法

找到 ~/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_launcher_icons-0.9.2/lib/android.dart 文件修改內(nèi)容如下

// 原有內(nèi)容
// final String minSdk = line.replaceAll(RegExp(r'[^\d]'), '');
// 替換內(nèi)容
final String minSdk = "21";

注意:如果使用了鏡像地址就找到 ~flutter/.pub-cache/hosted/pub.flutter-io.cn/flutter_launcher_icons-0.9.2/lib/android.dart 文件來修改以上內(nèi)容

解決辦法參考地址

https://github.com/fluttercommunity/flutter_launcher_icons/issues/324

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容