关于flutter:在Flutter中动态地改变应用启动器图标

6次阅读

共计 2392 个字符,预计需要花费 6 分钟才能阅读完成。

在本文中,咱们将探讨如何在 Flutter 应用程序的运行时动静更改多个应用程序启动器图标。

在 pubspec.yaml 文件中增加以下依赖项。

flutter_dynamic_icon:https://pub.dev/packages/flutter_dynamic_icon

思考咱们曾经筹备好根本的 UI(蕴含图像和按钮小部件)。

import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int iconIndex = 0;

  List iconName = <String>['icon1', 'icon2', 'icon3'];
  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: buildAppBar(appBarTitle: widget.title),
      body: Padding(padding: EdgeInsets.all(kSpacing),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [buildIconTile(0, 'red'),
              buildIconTile(1, 'dark'),
              buildIconTile(2, 'blue'),
              HeightSpacer(myHeight: kSpacing),
              PrimaryBtn(btnFun: () => changeAppIcon(), btnText: 'Set as app icon'),
            ],
          )),
    );
  }

  Widget buildIconTile(int index, String themeTxt) => Padding(padding: EdgeInsets.all(kSpacing / 2),
        child: GestureDetector(onTap: () => setState(() => iconIndex = index),
          child: ListTile(contentPadding: const EdgeInsets.only(left: 0.0, right: 0.0),
              leading: Image.asset(imagefiles[index],
                width: 45,
                height: 45,
              ),
              title: Text(themeTxt, style: const TextStyle(fontSize: 25)),
              trailing: iconIndex == index
                  ? const Icon(
                      Icons.check_circle_rounded,
                      color: Colors.green,
                      size: 30,
                    )
                  : Icon(
                      Icons.circle_outlined,
                      color: Colors.grey.withOpacity(0.5),
                      size: 30,
                    )),
        ),
      );

  changeAppIcon()  {}

当初咱们须要在按钮小部件的 onpress 事件 [changeAppIcon{}] 中编写更改利用启动器图标的逻辑。

changeAppIcon() async {
  try {if (await FlutterDynamicIcon.supportsAlternateIcons) {await FlutterDynamicIcon.setAlternateIconName(iconName[iconIndex]);
      debugPrint("App icon change successful");
      return;
    }
  } catch (e) {debugPrint("Exception: ${e.toString()}");
  }
  debugPrint("Failed to change app icon");
}

至此,咱们实现了配置动静利用图标的编码局部。

当初要使该性能失常工作,咱们须要在我的项目的 ios 文件夹中的 info.plist 文件中增加一些更改。

因而,咱们须要通过右键单击 ios 文件夹在 xCode 中关上我的项目。

⚠️(留神:此性能针对 iOS 平台,因而咱们须要 macOS 设施进行设置)。

在 xCode 中关上我的项目后,尝试在 Runner/Runner 文件夹中增加应用程序图标图像,如下所示。

接下来咱们须要设置 info.plist 文件(依照上面给出的步骤)。

👉将 Icon files (iOS 5) 增加到信息属性列表。

👉将 CFBundleAlternateIcons 作为一个字典增加到下面创立的 icon files(ios 5) 中。

👉在 CFBundleAlternateIcons 下创立 3 个字典,其名称与图标图像文件名类似(在咱们的例子中是 icon1icon2icon3)。

👉对于每个字典(icon1icon2icon3),须要创立两个属性——UIPrerenderedIconCFBundleIconFiles

👉最初将 CFBundleIconFiles 改成一个 array,并将 item0 的值增加到各自的字典中,作为 icon1icon2icon3

当初运行命令行

flutter clean
flutter pub get

就是这样。🎉 运行代码以查看它的实际效果。🥳

请参阅我的视频教程以获取残缺指南:https://www.youtube.com/watch…
在此处获取残缺的源代码:https://github.com/vijayinyou…

正文完
 0