flutter_xupdate 0.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 83

flutter_xupdate #

Version Build Status Issue Star

A Flutter plugin for XUpdate -- Android Update Library。

Getting Started #

You should ensure that you add the flutter_xupdate as a dependency in your flutter project.

// pub 集成
dependencies:
  flutter_xupdate: ^0.0.3

//github  集成
dependencies:
  flutter_xupdate:
    git:
      url: git://github.com/xuexiangjys/flutter_xupdate.git
      ref: master

Setting up #

Modify the Main App Theme to AppCompat,For example:

<resources>
    <style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/launch_background</item>
    </style>
</resources>

UseCase #

Initialization #

  ///初始化
   void initXUpdate() {
     if (Platform.isAndroid) {
       FlutterXUpdate.init(
         ///是否输出日志
         debug: true,
         ///是否使用post请求
         isPost: false,
         ///post请求是否是上传json
         isPostJson: false,
         ///是否开启自动模式
         isWifiOnly: false,
         ///是否开启自动模式
         isAutoMode: false,
         ///需要设置的公共参数
         supportSilentInstall: false,
         ///在下载过程中,如果点击了取消的话,是否弹出切换下载方式的重试提示弹窗
         enableRetry: false
       ).then((value) {
         updateMessage("初始化成功: $value");
       }).catchError((error) {
         print(error);
       });

       FlutterXUpdate.setErrorHandler(
           onUpdateError: (Map<String, dynamic> message) async {
         print(message);
         setState(() {
           _message = "$message";
         });
       });
     } else {
       updateMessage("ios暂不支持XUpdate更新");
     }
   }

JSON Format #

{
  "Code": 0, //0代表请求成功,非0代表失败
  "Msg": "", //请求出错的信息
  "UpdateStatus": 1, //0代表不更新,1代表有版本更新,不需要强制升级,2代表有版本更新,需要强制升级
  "VersionCode": 3,
  "VersionName": "1.0.2",
  "ModifyContent": "1、优化api接口。\r\n2、添加使用demo演示。\r\n3、新增自定义更新服务API接口。\r\n4、优化更新提示界面。",
  "DownloadUrl": "https://raw.githubusercontent.com/xuexiangjys/XUpdate/master/apk/xupdate_demo_1.0.2.apk",
  "ApkSize": 2048
  "ApkMd5": "..."  //md5值没有的话,就无法保证apk是否完整,每次都会重新下载。框架默认使用的是md5加密。
}

CheckUpdate #

  ///默认App更新
  void checkUpdateDefault() {
    FlutterXUpdate.checkUpdate(url: _updateUrl);
  }

  ///默认App更新 + 支持后台更新
  void checkUpdateSupportBackground() {
    FlutterXUpdate.checkUpdate(url: _updateUrl, supportBackgroundUpdate: true);
  }

  ///调整宽高比
  void checkUpdateRatio() {
    FlutterXUpdate.checkUpdate(url: _updateUrl, widthRatio: 0.6);
  }

  ///强制更新
  void checkUpdateForce() {
    FlutterXUpdate.checkUpdate(url: mUpdateUrl2);
  }

  ///自动模式, 如果需要完全无人干预,自动更新,需要root权限【静默安装需要】
  void checkUpdateAutoMode() {
    FlutterXUpdate.checkUpdate(url: _updateUrl, isAutoMode: true);
  }

  ///下载时点击取消允许切换下载方式
  void enableChangeDownLoadType() {
    FlutterXUpdate.checkUpdate(
      url: _updateUrl,
      overrideGlobalRetryStrategy: true,
      enableRetry: true,
      retryContent: "Github下载速度太慢了,是否考虑切换蒲公英下载?",
      retryUrl: "https://www.pgyer.com/flutter_learn");
  }

Custom JSON Format #

1.Setting up a custom update parser

FlutterXUpdate.setCustomParseHandler(onUpdateParse: (String json) async {
//Here is the custom JSON parsing
return customParseJson(json);
});

///Resolve the custom JSON content to the UpdateEntity entity class
UpdateEntity customParseJson(String json) {
  AppInfo appInfo = AppInfo.fromJson(json);
  return UpdateEntity(
      hasUpdate: appInfo.hasUpdate,
      isIgnorable: appInfo.isIgnorable,
      versionCode: appInfo.versionCode,
      versionName: appInfo.versionName,
      updateContent: appInfo.updateLog,
      downloadUrl: appInfo.apkUrl,
      apkSize: appInfo.apkSize);
}

2.Set the parameter isCustomParse to true

FlutterXUpdate.checkUpdate(url: _updateUrl3, isCustomParse: true);

Update By UpdateEntity Directly #

///直接传入UpdateEntity进行更新提示
void checkUpdate8() {
    FlutterXUpdate.updateByInfo(updateEntity: customParseJson(_customJson));
}

Custom Update Prompt Style #

Currently, only theme color and top picture customization are supported!

1.Configure top picture, Path: android/app/src/main/res/values/drawable, For example:

2.Set the parameter themeColor and topImageRes

///自定义更新弹窗样式
void customPromptDialog() {
    FlutterXUpdate.checkUpdate(url: _updateUrl, themeColor: '#FFFFAC5D', topImageRes: 'bg_update_top');
}

0.0.3 #

  • Add custom update prompt style function.

0.0.2 #

  • Add custom JSON parsing function.
  • Add update By UpdateEntity directly function.

0.0.1 #

  • The first version.

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_xupdate/flutter_xupdate.dart';
import 'package:flutter_xupdate_example/app_info.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

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

class _MyAppState extends State<MyApp> {
  String _message = '';

  String _customJson = '';

  @override
  void initState() {
    super.initState();
    initXUpdate();
    loadJsonFromAsset();
  }

  Future<void> loadJsonFromAsset() async {
    _customJson = await rootBundle.loadString('assets/update_custom.json');
  }

  ///初始化
  void initXUpdate() {
    if (Platform.isAndroid) {
      FlutterXUpdate.init(

              ///是否输出日志
              debug: true,

              ///是否使用post请求
              isPost: false,

              ///post请求是否是上传json
              isPostJson: false,

              ///是否开启自动模式
              isWifiOnly: false,

              ///是否开启自动模式
              isAutoMode: false,

              ///需要设置的公共参数
              supportSilentInstall: false,

              ///在下载过程中,如果点击了取消的话,是否弹出切换下载方式的重试提示弹窗
              enableRetry: false)
          .then((value) {
        updateMessage("初始化成功: $value");
      }).catchError((error) {
        print(error);
      });

//      FlutterXUpdate.setErrorHandler(
//          onUpdateError: (Map<String, dynamic> message) async {
//        print(message);
//        //下载失败
//        if (message["code"] == 4000) {
//          FlutterXUpdate.showRetryUpdateTipDialog(
//              retryContent: "Github被墙无法继续下载,是否考虑切换蒲公英下载?",
//              retryUrl: "https://www.pgyer.com/flutter_learn");
//        }
//        setState(() {
//          _message = "$message";
//        });
//      });

//      FlutterXUpdate.setCustomParseHandler(onUpdateParse: (String json) async {
//        //这里是自定义json解析
//        return customParseJson(json);
//      });

      FlutterXUpdate.setUpdateHandler(
          onUpdateError: (Map<String, dynamic> message) async {
        print(message);
        //下载失败
        if (message["code"] == 4000) {
          FlutterXUpdate.showRetryUpdateTipDialog(
              retryContent: "Github被墙无法继续下载,是否考虑切换蒲公英下载?",
              retryUrl: "https://www.pgyer.com/flutter_learn");
        }
        setState(() {
          _message = "$message";
        });
      }, onUpdateParse: (String json) async {
        //这里是自定义json解析
        return customParseJson(json);
      });
    } else {
      updateMessage("ios暂不支持XUpdate更新");
    }
  }

  void updateMessage(String message) {
    setState(() {
      _message = message;
    });
  }

  ///将自定义的json内容解析为UpdateEntity实体类
  UpdateEntity customParseJson(String json) {
    AppInfo appInfo = AppInfo.fromJson(json);
    print(appInfo);
    return UpdateEntity(
        hasUpdate: appInfo.hasUpdate,
        isIgnorable: appInfo.isIgnorable,
        versionCode: appInfo.versionCode,
        versionName: appInfo.versionName,
        updateContent: appInfo.updateLog,
        downloadUrl: appInfo.apkUrl,
        apkSize: appInfo.apkSize);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('flutter_xupdate Demo'),
        ),
        body: SingleChildScrollView(
            padding: EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start, //文本是起始端对齐
              children: <Widget>[
                Container(
                  alignment: Alignment.topLeft,
                  height: 150,
                  color: Colors.grey,
                  padding: EdgeInsets.all(10),
                  child: Text(_message,
                      style: TextStyle(color: Colors.white, fontSize: 12)),
                ),
                autoFitWidget(ButtonBar(
                  alignment:
                      MainAxisAlignment.start, //布局方向,默认MainAxisAlignment.end
                  mainAxisSize: MainAxisSize.min, //主轴大小,默认MainAxisSize.max
                  children: <Widget>[
                    RaisedButton(
                      child: Text('默认App更新'),
                      color: Colors.blue,
                      onPressed: checkUpdateDefault,
                    ),
                    RaisedButton(
                      child: Text('默认App更新 + 支持后台更新'),
                      color: Colors.blue,
                      onPressed: checkUpdateSupportBackground,
                    ),
                  ],
                )),
                autoFitWidget(ButtonBar(
                  alignment:
                      MainAxisAlignment.start, //布局方向,默认MainAxisAlignment.end
                  mainAxisSize: MainAxisSize.min, //主轴大小,默认MainAxisSize.max
                  children: <Widget>[
                    RaisedButton(
                      child: Text('调整宽高比'),
                      color: Colors.blue,
                      onPressed: checkUpdateRatio,
                    ),
                    RaisedButton(
                      child: Text('强制更新'),
                      color: Colors.blue,
                      onPressed: checkUpdateForce,
                    ),
                    RaisedButton(
                      child: Text('自动模式'),
                      color: Colors.blue,
                      onPressed: checkUpdateAutoMode,
                    ),
                  ],
                )),
                autoFitWidget(ButtonBar(
                  alignment:
                      MainAxisAlignment.start, //布局方向,默认MainAxisAlignment.end
                  mainAxisSize: MainAxisSize.min, //主轴大小,默认MainAxisSize.max
                  children: <Widget>[
                    RaisedButton(
                      child: Text('下载时点击取消允许切换下载方式'),
                      color: Colors.blue,
                      onPressed: enableChangeDownLoadType,
                    ),
                    RaisedButton(
                      child: Text('显示重试提示弹窗'),
                      color: Colors.blue,
                      onPressed: showRetryDialogTip,
                    ),
                  ],
                )),
                autoFitWidget(ButtonBar(
                  alignment:
                      MainAxisAlignment.start, //布局方向,默认MainAxisAlignment.end
                  mainAxisSize: MainAxisSize.min, //主轴大小,默认MainAxisSize.max
                  children: <Widget>[
                    RaisedButton(
                      child: Text('使用自定义json解析'),
                      color: Colors.blue,
                      onPressed: customJsonParse,
                    ),
                    RaisedButton(
                      child: Text('直接传入UpdateEntity进行更新'),
                      color: Colors.blue,
                      onPressed: checkUpdateByUpdateEntity,
                    ),
                  ],
                )),
                autoFitWidget(ButtonBar(
                  alignment:
                  MainAxisAlignment.start, //布局方向,默认MainAxisAlignment.end
                  mainAxisSize: MainAxisSize.min, //主轴大小,默认MainAxisSize.max
                  children: <Widget>[
                    RaisedButton(
                      child: Text('自定义更新弹窗样式'),
                      color: Colors.blue,
                      onPressed: customPromptDialog,
                    ),
                  ],
                ))
              ],
            )),
      ),
    );
  }

  Widget autoFitWidget(Widget child) {
    return Platform.isAndroid ? child : SizedBox();
  }

  String _updateUrl =
      "https://gitee.com/xuexiangjys/XUpdate/raw/master/jsonapi/update_test.json";

  String _updateUrl2 =
      "https://gitee.com/xuexiangjys/XUpdate/raw/master/jsonapi/update_forced.json";

  String _updateUrl3 =
      "https://gitee.com/xuexiangjys/XUpdate/raw/master/jsonapi/update_custom.json";

  ///默认App更新
  void checkUpdateDefault() {
    FlutterXUpdate.checkUpdate(url: _updateUrl);
  }

  ///默认App更新 + 支持后台更新
  void checkUpdateSupportBackground() {
    FlutterXUpdate.checkUpdate(url: _updateUrl, supportBackgroundUpdate: true);
  }

  ///调整宽高比
  void checkUpdateRatio() {
    FlutterXUpdate.checkUpdate(url: _updateUrl, widthRatio: 0.6);
  }

  ///强制更新
  void checkUpdateForce() {
    FlutterXUpdate.checkUpdate(url: _updateUrl2);
  }

  ///自动模式, 如果需要完全无人干预,自动更新,需要root权限【静默安装需要】
  void checkUpdateAutoMode() {
    FlutterXUpdate.checkUpdate(url: _updateUrl, isAutoMode: true);
  }

  ///下载时点击取消允许切换下载方式
  void enableChangeDownLoadType() {
    FlutterXUpdate.checkUpdate(
        url: _updateUrl,
        overrideGlobalRetryStrategy: true,
        enableRetry: true,
        retryContent: "Github下载速度太慢了,是否考虑切换蒲公英下载?",
        retryUrl: "https://www.pgyer.com/flutter_learn");
  }

  ///显示重试提示弹窗
  void showRetryDialogTip() {
    FlutterXUpdate.showRetryUpdateTipDialog(
        retryContent: "Github下载速度太慢了,是否考虑切换蒲公英下载?",
        retryUrl: "https://www.pgyer.com/flutter_learn");
  }

  ///使用自定义json解析
  void customJsonParse() {
    FlutterXUpdate.checkUpdate(url: _updateUrl3, isCustomParse: true);
  }

  ///直接传入UpdateEntity进行更新提示
  void checkUpdateByUpdateEntity() {
    FlutterXUpdate.updateByInfo(updateEntity: customParseJson(_customJson));
  }


  ///自定义更新弹窗样式
  void customPromptDialog() {
    FlutterXUpdate.checkUpdate(url: _updateUrl, themeColor: '#FFFFAC5D', topImageRes: 'bg_update_top');
  }

}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_xupdate: ^0.0.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_xupdate/flutter_xupdate.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
70
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
83
Learn more about scoring.

We analyzed this package on Apr 6, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test