Flutter自定义应用图标插件custom_app_logo的使用

发布于 1周前 作者 bupafengyu 来自 Flutter

Flutter自定义应用图标插件custom_app_logo的使用

Introduction(简介)

什么是custom_app_logo?

此插件用于动态更改Android平台上的应用图标。

为什么使用custom_app_logo?

如果你的应用需要在特定情况下动态更改应用图标,这个插件将非常有用。

Installation(安装)

pubspec.yaml文件中添加以下依赖:

dependencies:
  custom_app_logo: [latest-version]

Setup and Usage(设置与使用)

以下是使用步骤:

  1. 将你的XML图标添加到android/app/src/main/res/...文件夹中。
  2. 添加一个新的<activity-alias/>
  3. 在新增的<activity-alias/>中添加图标。

示例代码如下:

<activity
    android:icon="@drawable/ic_yellow"
    android:name=".MainActivity"
    android:enabled="true"
    android:exported="true"
    android:launchMode="singleTop"
    android:theme="@style/LaunchTheme"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize">
    <meta-data
        android:name="io.flutter.embedding.android.NormalTheme"
        android:resource="@style/NormalTheme"/>
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>
<activity-alias
    android:enabled="true"
    android:icon="@drawable/ic_green"
    android:exported="true"
    android:targetActivity=".MainActivity"
    android:name=".SecondActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity-alias>

注意:你需要在<activity android:name=".MainActivity"/>中包含一个图标。

  1. 调用函数:
await _customAppLogoPlugin.getActivities();

这将返回一个List<ActivityModel>

  1. 每个ActivityModel包含以下内容:
class ActivityModel {
  final String activtiyName;

  final Uint8List activtiyLogo;

  final VoidCallback onChangeActivtiyLogo;

  ActivityModel({required this.activtiyName, required this.activtiyLogo, required this.onChangeActivtiyLogo});
}

只需调用onChangeActivtiyLogo回调函数即可更改图标。

注意:iOS的实现将在以后添加。

Example(示例)

以下是一个完整的示例代码:

import 'package:custom_app_logo/activityModel.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:custom_app_logo/custom_app_logo.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _customAppLogoPlugin = CustomAppLogo();
  List<ActivityModel> _activties = [];

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 异步初始化平台状态
  Future<void> initPlatformState() async {
    final result = await _customAppLogoPlugin.getActivities();
    setState(() {
      _activties = result;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: ListView.builder(
            itemCount: _activties.length,
            itemBuilder: (context, index) {
              return ListTile(
                onTap: () => showDialog(
                  context: context,
                  builder: (context) => Dialog(
                    child: Padding(
                      padding: EdgeInsets.all(30),
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          const Text(
                            "Are you sure you want to change to this Logo ?",
                            style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                          ),
                          Row(
                            children: [
                              TextButton(
                                onPressed: () {
                                  _activties[index].onChangeActivtiyLogo();
                                  Navigator.pop(context);
                                },
                                child: Text("OK"),
                              ),
                              TextButton(
                                onPressed: () => Navigator.pop(context),
                                child: Text("Cancel"),
                              ),
                            ],
                          )
                        ],
                      ),
                    ),
                  ),
                ),
                title: Text(_activties[index].activtiyName, style: TextStyle(fontSize: 14),),
                leading: Image.memory(_activties[index].activtiyLogo),
              );
            },
          ),
        ),
      ),
    );
  }
}

更多关于Flutter自定义应用图标插件custom_app_logo的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义应用图标插件custom_app_logo的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中,如果你想自定义应用图标,通常需要手动更新Android和iOS项目的配置文件。不过,有一些插件可以帮助你简化这个过程。custom_app_logo 是其中一个插件,它可以让你更方便地设置应用图标。

安装 custom_app_logo 插件

首先,你需要在 pubspec.yaml 文件中添加 custom_app_logo 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  custom_app_logo: ^1.0.0  # 请查看最新版本号

然后运行 flutter pub get 来安装插件。

使用 custom_app_logo 插件

安装完插件后,你可以在 main.dart 文件中使用它来设置应用图标。

import 'package:flutter/material.dart';
import 'package:custom_app_logo/custom_app_logo.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Custom App Logo Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
      // 设置自定义应用图标
      onGenerateTitle: (BuildContext context) {
        CustomAppLogo.setAppLogo('assets/logo.png'); // 设置应用图标
        return 'Custom App Logo';
      },
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom App Logo Example'),
      ),
      body: Center(
        child: Text('Hello, Custom App Logo!'),
      ),
    );
  }
}

配置应用图标

  1. 准备图标文件:确保你有一个合适尺寸的图标文件(例如 logo.png),并将其放在 assets 目录下。

  2. 更新 pubspec.yaml:确保在 pubspec.yaml 文件中声明了你的图标文件:

flutter:
  assets:
    - assets/logo.png
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!