Flutter版本更新插件update_version的使用

Flutter版本更新插件update_version的使用

update_version 是一个用于检测和提示用户更新应用的新版 Flutter 插件。它可以检查当前应用版本与应用商店版本之间的差异,并在有新版本时弹出提示框,引导用户前往应用商店更新。

Getting Started(开始使用)

项目介绍

这是一个基于 Dart 的 Flutter 包,包含可以轻松共享到多个 Flutter 或 Dart 项目的库模块代码。

使用帮助

要开始使用 Flutter,请查看我们的 在线文档,其中包含教程、示例、移动开发指南和完整的 API 参考。


示例代码

以下是一个完整的示例代码,展示如何使用 update_version 插件来检测并提示用户更新应用。

示例代码文件:example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:update_version/update_version.dart'; // 导入 update_version 插件

void main() {
  runApp(MyApp()); // 启动应用
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(), // 设置主页
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState(); // 初始化状态
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    checkVersion(); // 在初始化时调用版本检查函数
  }

  // 检查版本更新的函数
  Future checkVersion() async {
    VersionStatus? versionStatus = await new UpdateVersion().getVersionStatus(); // 获取版本状态
    if (versionStatus != null && versionStatus.canUpdate == true) { // 如果有新版本
      showDialog( // 弹出对话框提示用户更新
        context: context,
        barrierDismissible: true, // 点击背景可关闭对话框
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('需要更新'), // 对话框标题
            content: Text('您可以从版本 ${versionStatus.localVersion} 更新到版本 ${versionStatus.storeVersion}'), // 显示版本信息
            actions: [
              TextButton( // "稍后" 按钮
                onPressed: () {
                  Navigator.of(context, rootNavigator: true).pop(); // 关闭对话框
                },
                child: Text('稍后'),
              ),
              TextButton( // "更新" 按钮
                onPressed: () {
                  new UpdateVersion().launchAppStore(versionStatus.appStoreLink); // 跳转到应用商店
                },
                child: Text('更新'),
              ),
            ],
          );
        },
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("示例应用"), // 应用标题
      ),
    );
  }
}

功能说明

  1. 导入插件

    import 'package:update_version/update_version.dart';
    

    将插件导入到项目中。

  2. 检查版本

    Future checkVersion() async {
      VersionStatus? versionStatus = await new UpdateVersion().getVersionStatus();
      if (versionStatus != null && versionStatus.canUpdate == true) {
        // 如果有新版本,则弹出提示框
      }
    }
    

    使用 getVersionStatus() 方法获取当前版本与商店版本的比较结果。

  3. 弹出对话框

    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('需要更新'),
          content: Text('您可以从版本 ${versionStatus.localVersion} 更新到版本 ${versionStatus.storeVersion}'),
          actions: [
            TextButton(onPressed: () { Navigator.pop(context); }, child: Text('稍后')),
            TextButton(onPressed: () { new UpdateVersion().launchAppStore(versionStatus.appStoreLink); }, child: Text('更新')),
          ],
        );
      }
    );
    

    提示用户更新应用,并提供“稍后”和“更新”两个选项。

  4. 跳转到应用商店

    new UpdateVersion().launchAppStore(versionStatus.appStoreLink);
    

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

1 回复

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


update_version 是一个用于 Flutter 应用程序的插件,它可以帮助开发者检查应用是否有新版本,并提示用户进行更新。以下是如何使用 update_version 插件的步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 update_version 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  update_version: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 导入插件

在需要使用 update_version 的 Dart 文件中导入插件:

import 'package:update_version/update_version.dart';

3. 初始化并检查更新

在你的代码中初始化 UpdateVersion 并检查是否有新版本。通常你可以在应用的启动页面(如 main.dart 中的 initStatedidChangeDependencies)中进行检查。

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    checkForUpdate();
  }

  Future<void> checkForUpdate() async {
    final updateVersion = UpdateVersion(
      androidId: 'com.example.app', // 替换为你的 Android 应用 ID
      iOSId: '123456789',          // 替换为你的 iOS 应用 ID
    );

    final updateInfo = await updateVersion.getUpdateInfo();

    if (updateInfo.needsUpdate) {
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: Text('有新版本可用'),
          content: Text('请更新到最新版本以获得最佳体验。'),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text('稍后再说'),
            ),
            TextButton(
              onPressed: () {
                updateVersion.launchAppStore();
                Navigator.of(context).pop();
              },
              child: Text('立即更新'),
            ),
          ],
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

4. 处理更新逻辑

在上面的代码中,checkForUpdate 方法会检查是否有新版本。如果有新版本,它会弹出一个对话框提示用户更新。用户可以选择立即更新或稍后再说。

updateVersion.launchAppStore() 方法会打开应用商店,以便用户下载最新版本。

5. 配置应用商店链接

确保你的应用已经在 Google Play 和 Apple App Store 上发布,并且 androidIdiOSId 配置正确。

6. 测试

在不同的设备和平台上测试你的应用,确保更新提示功能正常工作。

7. 处理其他情况

你还可以根据 updateInfo 中的其他信息(如当前版本号、最新版本号等)自定义更新逻辑。

例如:

if (updateInfo.needsUpdate) {
  print('当前版本: ${updateInfo.currentVersion}');
  print('最新版本: ${updateInfo.newVersion}');
  // 自定义逻辑
}
回到顶部