Flutter应用更新管理插件zupdate的使用

Flutter应用更新管理插件zupdate的使用

zupdate 是一个用于Flutter应用更新管理的简单插件。在Android上,它会下载文件(并报告进度)并触发应用程序安装意图。在iOS上,它会打开Safari并指定IPA URL。

你可以参考以下项目:

https://github.com/hacjy/zupdate/tree/master/example

重要变更

  • 提供了一套美观的UI更新。

  • 提供了一个用于安装APK的Flutter插件install_plugin;对于iOS,通过URL跳转到应用商店。

  • 必需设置

    1. pubspec.yaml文件中添加依赖项:
      dependencies:
        get: ^4.6.5
        flutter_screenutil: ^5.5.3+2
        zupdate: ^0.4.0
      
    2. main.dart文件中,根节点需要使用以下代码:
      // 根结点要要使用ScreenUtilInit和getx的GetMaterialApp
      return ScreenUtilInit(
          designSize: const Size(1920, 1080),
          minTextAdapt: true,
          splitScreenMode: true,
          builder: (context, Widget? widget) {
            return GetMaterialApp(
              home: Scaffold(
                appBar: AppBar(
                  title: const Text('VersionUpdate Plugin App'),
                ),
                body: GestureDetector(
                  onTap: () {
                    UpdateVersion.appUpdate(context);
                  },
                  child: const Center(
                    child: SizedBox(
                      width: 100,
                      height: 100,
                      child: Text('Check Update'),
                    ),
                  ),
                ),
              ),
            );
          });
      
    3. pubspec.yaml文件中添加以下代码:
      assets:
        - assets/
        - assets/img/
      
      然后将update_bg_app_top.pngupdate_ic_close.png 添加到assets/img目录下。这些图片是从示例中的assets/img复制过来的。
  • 支持自定义头部图片、按钮主题色、进度条颜色、标题、标题与头部图片的高度差、APK文件名以及是否显示中文文本:

    UpdateConfig(
         apkName: 'test.apk',
         title: 'test update version',
         themeColor: Colors.blueAccent,
         progressBackgroundColor: Colors.blue.withOpacity(0.3),
         extraHeight: 10,
         chLanguage: true)
    

使用方法

要在你的项目中使用此插件,请将其作为依赖项添加到pubspec.yaml文件中:

dependencies:
  zupdate: ^0.4.0

或者从GitHub集成:

dependencies:
  zupdate:
    git:
      url: git://github.com/hacjy/zupdate.git
      ref: master

预览

截图1 截图2 截图3 截图4

示例代码

// 导入包
import 'package:zupdate/version_xupdate/update/entity/update_entity.dart';
import 'package:zupdate/version_xupdate/update/flutter_update.dart';

// 版本更新
static Future<void> update(BuildContext context, Map<String, dynamic> appInfo) async {
  try {
    final url = appInfo['update_url'];
    UpdateEntity entity = UpdateEntity(
        isForce: appInfo['update_type'] == 1,
        hasUpdate: true,
        isIgnorable: false,
        versionCode: appInfo['version_code'],
        versionName: appInfo['version_name'],
        updateContent: appInfo['update_content'],
        apkMd5: appInfo['app_md5'] ?? '',
        // apkSize: appInfo['package_size'],
        downloadUrl: appInfo['update_url']);
    UpdateManager.checkUpdate(context, entity,
        // 支持自定义头部图片,按钮主题色,进度条颜色,标题,标题距离头部图片的高度,apk文件名,是否显示中文文本
        // config: UpdateConfig(
            // apkName: 'test.apk',
            // title: 'test update version',
            // themeColor: Colors.blueAccent,
            // progressBackgroundColor: Colors.blue.withOpacity(0.3),
            // extraHeight: 10,
            // chLanguage: true)
    );
  } catch (e) {
    print(e);
  }
}

安装插件示例

// 导入包
import 'package:zupdate/install_plugin.dart';

// 安装APK
static void installAPP(String uri) async {
  if (Platform.isAndroid) {
    String packageName = await CommonUtils.getPackageName();
    InstallPlugin.installApk(uri, packageName);
  } else {
    InstallPlugin.gotoAppStore(uri);
  }
}

完整示例代码

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:zupdate_example/update_version.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    // 根结点要要使用ScreenUtilInit和getx的GetMaterialApp
    return ScreenUtilInit(
        designSize: const Size(1920, 1080),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (context, Widget? widget) {
          return GetMaterialApp(
            home: Scaffold(
              appBar: AppBar(
                title: const Text('Zupdate Plugin Example'),
              ),
              body: Center(child: ElevatedButton(
                onPressed: () {
                  UpdateVersion.appUpdate(context);
                },
                child: Text('Check Update'),
              ),)
            ),
          );
        });
  }
}

更多关于Flutter应用更新管理插件zupdate的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter应用更新管理插件zupdate的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


zupdate 是一个用于 Flutter 应用更新的插件,它可以帮助开发者轻松地管理应用的版本更新。通过 zupdate,开发者可以检查应用是否有新版本,并提示用户进行更新。以下是 zupdate 的基本使用步骤:

1. 安装 zupdate 插件

首先,在 pubspec.yaml 文件中添加 zupdate 依赖:

dependencies:
  flutter:
    sdk: flutter
  zupdate: ^1.0.0  # 请检查最新版本

然后,运行 flutter pub get 来安装依赖。

2. 初始化 zupdate

在你的 main.dart 文件中,初始化 zupdate。通常,你可以在 main 函数中进行初始化:

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化 zupdate
  await ZUpdate.init(
    appId: 'your_app_id',  // 你的应用 ID
    baseUrl: 'https://your-update-server.com',  // 更新服务器的 URL
  );

  runApp(MyApp());
}

3. 检查更新

在应用的某个地方(例如,首页或设置页面),你可以调用 ZUpdate.checkForUpdate() 来检查是否有新版本:

void checkForUpdate() async {
  try {
    final updateInfo = await ZUpdate.checkForUpdate();
    if (updateInfo != null && updateInfo.hasUpdate) {
      // 如果有更新,显示更新对话框
      showUpdateDialog(updateInfo);
    }
  } catch (e) {
    print('检查更新失败: $e');
  }
}

void showUpdateDialog(UpdateInfo updateInfo) {
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('发现新版本'),
        content: Text('当前版本: ${updateInfo.currentVersion}\n新版本: ${updateInfo.newVersion}\n更新内容: ${updateInfo.updateDescription}'),
        actions: <Widget>[
          TextButton(
            child: Text('稍后再说'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
          TextButton(
            child: Text('立即更新'),
            onPressed: () {
              ZUpdate.performUpdate();  // 执行更新
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}

4. 处理更新

ZUpdate.performUpdate() 会启动应用的更新流程。根据平台不同,zupdate 会自动处理应用的更新。

  • Android: 会打开 Google Play 商店进行更新。
  • iOS: 会打开 App Store 进行更新。

5. 自定义配置

你可以在初始化时传入更多的配置参数来自定义 zupdate 的行为,例如:

  • forceUpdate: 是否强制用户更新。
  • showUpdateDialog: 是否显示默认的更新对话框。
await ZUpdate.init(
  appId: 'your_app_id',
  baseUrl: 'https://your-update-server.com',
  forceUpdate: true,  // 强制更新
  showUpdateDialog: false,  // 不显示默认对话框
);

6. 服务器端配置

zupdate 需要一个服务器来提供应用的版本信息。服务器需要返回一个 JSON 格式的响应,例如:

{
  "hasUpdate": true,
  "isForce": false,
  "newVersion": "1.1.0",
  "currentVersion": "1.0.0",
  "updateDescription": "修复了一些已知问题,提升了应用性能。",
  "apkUrl": "https://your-update-server.com/app.apk",  // Android 应用的下载链接
  "appStoreUrl": "https://apps.apple.com/app/id123456789"  // iOS 应用的 App Store 链接
}
回到顶部