当前位置:   article > 正文

Flutter 动态修改应用图标功能指南_flutter怎么改应用图标

flutter怎么改应用图标

Flutter 动态修改应用图标功能指南

视频

前言

原文 https://ducafecat.com/blog/flutter-dynamic-app-icons-guide

探索Flutter中动态应用图标的实现方法,了解如何为用户提供独特体验,促进用户升级和应用内购买。

默认图标

VIP 图标

参考

https://pub.dev/packages/flutter_dynamic_icon

https://pub-web.flutter-io.cn/packages/flutter_launcher_icons

准备3种APP图标

https://icon.kitchen

设置APP图标

https://pub-web.flutter-io.cn/packages/flutter_launcher_icons

保存图片

assets

pubspec.yaml

dev_dependencies:
	flutter_launcher_icons: ^0.13.1
  • 1
  • 2
flutter_icons:
  android: true
  ios: true
  image_path_ios: "assets/ios.png"
  image_path_android: "assets/android.png"
  • 1
  • 2
  • 3
  • 4
  • 5

运行生成图表

$ flutter pub run flutter_launcher_icons:main
  • 1

保存 Xcode 图标

图标尺寸

@2x 120x120

@3x 180x180

拖动到 xcode 目录

copy files

图标目录

xcode appicons

ios/Runner/Info.plist

<key>CFBundleIcons</key>
		<dict>
			<key>CFBundlePrimaryIcon</key>
			<dict>
				<key>CFBundleIconFiles</key>
				<array>
					<string>default</string>
				</array>
				<key>UIPrerenderedIcon</key>
				<false />
			</dict>
			<key>CFBundleAlternateIcons</key>
			<dict>
				<key>default</key>
				<dict>
					<key>CFBundleIconFiles</key>
					<array>
						<string>default</string>
					</array>
					<key>UIPrerenderedIcon</key>
					<false />
				</dict>
				<key>vip</key>
				<dict>
					<key>CFBundleIconFiles</key>
					<array>
						<string>vip</string>
					</array>
					<key>UIPrerenderedIcon</key>
					<false />
				</dict>
				<key>svip</key>
				<dict>
					<key>CFBundleIconFiles</key>
					<array>
						<string>svip</string>
					</array>
					<key>UIPrerenderedIcon</key>
					<false />
				</dict>
			</dict>
		</dict>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

配置 default vip svip 三张图片

切换图标

安装包

pubspec.yaml

dependencies:
	flutter_dynamic_icon: ^2.1.0
  • 1
  • 2

读取动态图标名称

lib/icons.dart

String curIconName = "";
  • 1
  
  void initState() {
    // 读取动态图标名称
    FlutterDynamicIcon.getAlternateIconName().then((iconName) {
      setState(() {
        curIconName = iconName ?? "---";
      });
    });
    
    super.initState();
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

设置图标

  // 设置图标
  Future<void> setAlternateIcon(String iconName) async {
    try {
      if (await FlutterDynamicIcon.supportsAlternateIcons) {
        await FlutterDynamicIcon.setAlternateIconName(
          iconName,
          showAlert: false,
        );
        setState(() {
          curIconName = iconName;
        });
      }
    } on PlatformException catch (_) {
      print('Failed to change app icon');
    }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

视图界面

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Dynamic Icons'),
      ),
      body: _buildMain(),
    );
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  // 主视图
  Widget _buildMain() {
    return Center(
      child: Column(
        children: [
          // 图标名称
          Text(curIconName),

          // 设置图标
          ElevatedButton(
            onPressed: () {
              setAlternateIcon("default");
            },
            child: const Text('default'),
          ),
          ElevatedButton(
            onPressed: () {
              setAlternateIcon("vip");
            },
            child: const Text('Vip'),
          ),
          ElevatedButton(
            onPressed: () {
              setAlternateIcon("svip");
            },
            child: const Text('svip'),
          ),
        ],
      ),
    );
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

设置Badge

初始读取

int batchNumber = -1;
  • 1
  
  void initState() {
    // 读取 badge 数字
    FlutterDynamicIcon.getApplicationIconBadgeNumber().then((value) {
      setState(() {
        batchNumber = value;
      });
    });

    super.initState();
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

设置更新

  // 主视图
  Widget _buildMain() {
    return Center(
      child: Column(
        children: [
          ...

          // 动态 badge 数字
          Text("$batchNumber"),

          // 文本输入框
          TextField(
            onSubmitted: (value) async {
              await FlutterDynamicIcon.setApplicationIconBadgeNumber(
                  int.parse(value));
              setState(() {
                batchNumber = int.parse(value);
              });
            },
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

代码

https://github.com/ducafecat/flutter_develop_tips/tree/main/flutter_application_dynamic_icon

小结

本文通过创建图标、设置图标、保存 xcode 图标、动态设置图标、设置 Badge,让你的 App 更丰富,提供更好的用户体验。

感谢阅读本文

如果有什么建议,请在评论中让我知道。我很乐意改进。


flutter 学习路径

  • Flutter 优秀插件推荐 https://flutter.ducafecat.com
  • Flutter 基础篇1 - Dart 语言学习 https://ducafecat.com/course/dart-learn
  • Flutter 基础篇2 - 快速上手 https://ducafecat.com/course/flutter-quickstart-learn
  • Flutter 实战1 - Getx Woo 电商APP https://ducafecat.com/course/flutter-woo
  • Flutter 实战2 - 上架指南 Apple Store、Google Play https://ducafecat.com/course/flutter-upload-apple-google
  • Flutter 基础篇3 - 仿微信朋友圈 https://ducafecat.com/course/flutter-wechat
  • Flutter 实战3 - 腾讯 tim 即时通讯开发 https://ducafecat.com/course/flutter-tim

© 猫哥
ducafecat.com

end

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/778227
推荐阅读
  

闽ICP备14008679号