Giter VIP home page Giter VIP logo

flutter-app-upgrade's Introduction

官网地址:http://laomengit.com/plugin/upgrade.html

添加依赖

1、在pubspec.yaml中加入:

dependencies:
  flutter_app_upgrade: ^1.1.0

2、执行flutter命令获取包:

flutter pub get`

3、引入

import 'package:flutter_app_upgrade/flutter_app_upgrade.dart';

4、如果你需要支持Android平台,在./android/app/src/main/AndroidManifest.xml文件中配置provider,代码如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.flutter.laomeng.flutter_upgrade_example">
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="flutter_upgrade_example">
				...
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.flutter.laomeng.flutter_upgrade_example.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                tools:replace="android:resource"
                android:resource="@xml/file_paths" />
        </provider>
    </application>
</manifest>

注意:provider中authorities的值为当前App的包名,和顶部的package值保持一致。

App升级功能使用介绍

只需在主页的initState方法中调用升级检测方法:

@override
  void initState() {
    AppUpgrade.appUpgrade(
      context,
      _checkAppInfo(),
      iosAppId: 'id88888888',
    );
    super.initState();
  }

_checkAppInfo方法访问后台接口获取是否有新的版本的信息,返回Future<AppUpgradeInfo> 类型,AppUpgradeInfo包含title、升级内容、apk下载url、是否强制升级等版本信息。如果没有新的版本不返回即可。

iosAppId参数用于跳转到app store。

_checkAppInfo()方法通常是访问后台接口,这里直接返回新版本信息,代码如下:

 Future<AppUpgradeInfo> _checkAppInfo() async {
    //这里一般访问网络接口,将返回的数据解析成如下格式
    return Future.delayed(Duration(seconds: 1), () {
      return AppUpgradeInfo(
        title: '新版本V1.1.1',
        contents: [
          '1、支持立体声蓝牙耳机,同时改善配对性能',
          '2、提供屏幕虚拟键盘',
          '3、更简洁更流畅,使用起来更快',
          '4、修复一些软件在使用时自动退出bug',
          '5、新增加了分类查看功能'
        ],
        force: false,
      );
    });
  }

好了,基本的升级功能就完成了,弹出提示框的效果如下:

点击“以后再说”,提示框消失,点击“立即体验”,自动区分不同平台。

访问后台接口获取新版本的信息一般需要当前App的包名和版本,查询方法如下:

await FlutterUpgrade.appInfo

返回的类型是AppInfo

  • versionName:版本号,比如1.0.0。
  • versionCode:Android独有版本号,对应Android build.gradle中的versionCode,ios返回“0”。
  • packageName:包名,对应Android build.gradle中的applicationId,ios的BundleIdentifier。

iOS平台升级

iOS平台直接跳转到app store相关页面,iosAppId一定要设置对,否则app store会找不到应用程序。

Android平台下载apk

Android平台则会判断是否设置了apk下载url,如果设置了则下载apk则直接下载,效果如下:

当下载完成时直接跳转到apk安装引导界面,效果如下:

用户点击允许,出现如下界面:

点击继续安装即可,上面的安装引导界面是系统界面,不同的手机或者不同的Android版本会略有不同。

Android平台跳转应用市场

如果不提供apk下载地址,点击“立即体验”,则会跳转到应用市场,不指定应用市场则会弹出提示框,让用户选择应用市场,效果如下:

提示框内将会包含手机内安装的所有的应用市场,用户选择一个然后跳转到对应应用市场的详情界面,如果当前应用未在此市场上架则会出现“找不到的界面”。

通常情况下会指定应用市场,这就需要知道用户手机内安装的应用市场,查询方法如下:

_getInstallMarket() async {
  List<String> marketList = await FlutterUpgrade.getInstallMarket();
}

插件内置了国内常用的应用市场,包括小米、魅族、vivo、oppo、华为、zte、360助手、应用宝、pp助手、豌豆荚,如果你需要检测其他的应用市场,比如google play,只需添加googl play的包名即可:

_getInstallMarket() async {
  List<String> marketList = await FlutterUpgrade.getInstallMarket(marketPackageNames: ['google play 包名']);
}

方法返回手机安装的应用市场,根据安装的应用市场指定跳转应用市场,如果你要指定内置的应用市场,可以根据包名获取内置的应用市场的相关信息:

AppMarketInfo _marketInfo = AppMarket.getBuildInMarket(packageName);

指定华为应用市场:

AppUpgrade.appUpgrade(
  context,
  _checkAppInfo(),
  iosAppId: 'id88888888',
  appMarketInfo: AppMarket.huaWei
);

指定没有内置的应用市场方法如下:

AppUpgrade.appUpgrade(
  context,
  _checkAppInfo(),
  iosAppId: 'id88888888',
  appMarketInfo: AppMarketInfo(
    '应用市场名称(选填)','应用市场包名','应用市场类名'
  ),
);

用户行为和下载回调

新的版本(1.1.0)新增了取消立即更新回调,用法如下:

AppUpgrade.appUpgrade(
  context,
  _checkAppInfo(),
  cancelText: '以后再说',
  okText: '马上升级',
  iosAppId: 'id88888888',
  appMarketInfo: AppMarket.huaWei,
  onCancel: () {
    print('onCancel');
  },
  onOk: () {
    print('onOk');
  },
  
);

新增的下载进度下载状态变化回调,用法如下:

AppUpgrade.appUpgrade(
  context,
  _checkAppInfo(),
  cancelText: '以后再说',
  okText: '马上升级',
  iosAppId: 'id88888888',
  appMarketInfo: AppMarket.huaWei,
  downloadProgress: (count, total) {
    print('count:$count,total:$total');
  },
  downloadStatusChange: (DownloadStatus status, {dynamic error}) {
    print('status:$status,error:$error');
  },
);

提示框样式定制

如果默认的升级提示框不满足你的需求,那么你可以定制你的升级提示框。

title及升级内容文字样式设置:

AppUpgrade.appUpgrade(context, _checkAppInfo(),
    titleStyle: TextStyle(fontSize: 30),
    contentStyle: TextStyle(fontSize: 18),
    ...
)

通过titleStylecontentStyle设置其样式,可以设置字体大小、颜色、粗体等。

设置“取消”和“升级按钮”文本和样式:

AppUpgrade.appUpgrade(context, _checkAppInfo(),
    cancelText: '以后再说',
    cancelTextStyle: TextStyle(color: Colors.grey),
    okText: '马上升级',
    okTextStyle: TextStyle(color: Colors.red),
	...
)

默认“取消”按钮文本是"以后再说",默认“升级”按钮的文本是“立即体验”。

设置“升级”按钮的背景颜色,需要2种颜色,2种颜色左到右线性渐变,如果想设置纯色,只需将2种颜色设置为同一个颜色即可,默认颜色是系统的primaryColor:

AppUpgrade.appUpgrade(context, _checkAppInfo(),
    okBackgroundColors: [Colors.blue, Colors.lightBlue],
    ...
)

设置进度条的颜色:

AppUpgrade.appUpgrade(context, _checkAppInfo(),
    progressBarColor: Colors.lightBlue.withOpacity(.4),
    ...
)

设置升级提示框的圆角半径,默认是20:

AppUpgrade.appUpgrade(context, _checkAppInfo(),
    borderRadius: 15,
    ...
)

Flutter App 升级功能流程

应用程序升级功能是App的基础功能之一,如果没有此功能会造成用户无法升级,应用程序的bug或者新功能老用户无法触达,甚至损失这部分用户。

对于应用程序升级功能的重要性就无需赘言了,下面介绍下应用程序升级功能的几种方式,从平台方面来说:

  • IOS平台,应用程序升级功能只能通过跳转到app store进行升级。
  • Android平台,既可以通过跳转到应用市场进行升级,也可以下载apk包升级。

从强制性来说可以分别强制升级和非强制升级:

  • 强制升级:就是用户必须升级才能继续使用App,如果不是非常必要不建议使用如此强硬的方式,会造成用户的反感。
  • 非强制升级就是允许用户点击“取消”,继续使用App。

下面分别介绍IOS和Android升级流程。

IOS升级流程

IOS升级流程如下:

流程说明:

  1. 通常我们会访问后台接口获取是否有新的版本,如果有新的版本则弹出提示框,判断当前版本是否为“强制升级”,如果是则只提供用户一个“升级”的按钮,否则提供用户“升级”和“取消”按钮。
  2. 弹出提示框后用户选择是否升级,如果选择“取消”,提示框消失,如果选择“升级”,跳转到app store进行升级。

Android 升级流程

相比ios的升级过程,Android就稍显复杂了,流程图如下:

流程说明:

  1. 访问后台接口获取是否有新的版本,这里和IOS是一样的,有则弹出升级提示框,判断当前版本是否为“强制升级”,如果是则只提供用户一个“升级”的按钮,否则提供用户“升级”和“取消”按钮。
  2. 弹出提示框后有用户选择是否升级,如果选择“取消”,提示框消失,如果选择“升级”,判断是跳转到应用市场进行升级还是通过下载apk升级。
  3. 如果下载apk升级,则开始下载apk,下载完成后跳转到apk安装引导界面。
  4. 如果跳转到应用市场升级,判断是否指定了应用市场,比如只在华为应用市场上架了,那么此时需要指定跳转到华为应用市场,即使你在很多应用市场都上架了,也应该根据用户手机安装的应用市场指定一个应用市场,让用户选择应用市场不是一个好的体验,而且用户也不知道应该去哪个市场更新,如果用户选择了一个你没有上架的应用市场,那就更尴尬了。
  5. 指定应用市场后直接跳转到指定的应用市场的更新界面。

flutter-app-upgrade's People

Contributors

laomengflutter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

flutter-app-upgrade's Issues

请问下这是什么原因启动不成功

e: D:\flutter.pub-cache\hosted\pub.flutter-io.cn\flutter_app_upgrade-1.1.0\android\src\main\kotlin\com\flutter\flutter_app_upgrade\FlutterAppUpgradePlugin.kt: (62, 54): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type File?
e: D:\flutter.pub-cache\hosted\pub.flutter-io.cn\flutter_app_upgrade-1.1.0\android\src\main\kotlin\com\flutter\flutter_app_upgrade\FlutterAppUpgradePlugin.kt: (127, 20): None of the following functions can be called with the arguments supplied:
@nonnull public open fun setClassName(@nonnull p0: Context, @nonnull p1: String): Intent defined in android.content.Intent
@nonnull public open fun setClassName(@nonnull p0: String, @nonnull p1: String): Intent defined in android.content.Intent

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':flutter_app_upgrade:compileDebugKotlin'.

Compilation error. See log for more details

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 16s
Exception: Gradle task assembleDebug failed with exit code 1

Only safe (?.) or non-null asserted (!!.)

e: /Users/name/Library/flutter/.pub-cache/hosted/pub.flutter-io.cn/flutter_app_upgrade-1.1.0/android/src/main/kotlin/com/flutter/flutter_app_upgrade/FlutterAppUpgradePlugin.kt: (62, 54): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type File?
e: /Users/name/Library/flutter/.pub-cache/hosted/pub.flutter-io.cn/flutter_app_upgrade-1.1.0/android/src/main/kotlin/com/flutter/flutter_app_upgrade/FlutterAppUpgradePlugin.kt: (127, 20): None of the following functions can be called with the arguments supplied:
@nonnull public open fun setClassName(@nonnull p0: Context, @nonnull p1: String): Intent defined in android.content.Intent
@nonnull public open fun setClassName(@nonnull p0: String, @nonnull p1: String): Intent defined in android.content.Intent

FAILURE: Build failed with an exception.

[✓] Flutter (Channel stable, 2.5.2, on macOS 11.1 20C69 darwin-x64, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] Connected device (3 available)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.