修改Flutter程序图标

图标尺寸

  • andorid 512 x 512
  • ios 1024 x 1024

注:andorid 和 ios 都使用1024 x 1024 尺寸设计图标就可以。

在线工具

DesignEvo

使用插件

flutter_launcher_icons

插件使用步骤

在开发依赖中添加包

1
flutter pub add flutter_launcher_icons -D

配置图标文件

在 pubspec.yaml 文件中配置使用的图标文件

IOS和Android使用同一个图标文件的情况:

pubspec.yaml
1
2
3
4
5
6
7
dev_dependencies:
flutter_launcher_icons:
flutter_icons:
# android: "launcher_icon"
android: true
ios: true
image_path: "assets/icon/icon.png"

IOS和Android分别使用不同图标文件的情况:

pubspec.yaml
1
2
3
4
5
6
7
8
9
dev_dependencies:
flutter_launcher_icons:
flutter_icons:
ios: true
android: true
image_path_ios: "assets/launcher/icon.png"
image_path_android: "assets/launcher/icon.png"
adaptive_icon_background: "assets/launcher/background.png"
adaptive_icon_foreground: "assets/launcher/foreground.png"

生成图标

1
2
flutter pub get
flutter pub run flutter_launcher_icons:main

检查生成情况

  • android android/app/src/main/res
  • ios ios/Runner/Assets.xcassets/AppIcon.appiconset

埋坑

如果运行生成图标命令报如下错误

1
2
Unhandled exception:
FormatException: Invalid number (at character 1

处理方法1:

修改lutter_launcher_icons的文件: package:flutter_launcher_icons/android.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
int minSdk() {
final File androidGradleFile = File(constants.androidGradleFile);
final List<String> lines = androidGradleFile.readAsLinesSync();
for (String line in lines) {
if (line.contains('minSdkVersion')) {
// remove anything from the line that is not a digit
final String minSdk = line.replaceAll(RegExp(r'[^\d]'), '');
// return int.parse(minSdk);
return 21;
}
}
return 0; // Didn't find minSdk, assume the worst
}

处理方法2:

使用已经修复这个错误的包

1
2
3
4
flutter_launcher_icons:
git:
url: https://github.com/Davenchy/flutter_launcher_icons.git
ref: fixMinSdkParseFlutter2.8