flutter 更新apk

Dart 复制代码
import 'package:path_provider/path_provider.dart';   //app_installer: ^1.0.0
import 'package:app_installer/app_installer.dart';    //path_provider

 //查询是否更新
  Future<void> ifUpdate(BuildContext buildContext) async {
    String jsonFlag = await LocalStorage.get("apkJson");
    var s = jsonFlag;
    var dio = Dio();
    final response = await dio.get(s);
    print(response.data);
    if(response.data != null){
      setState(() {

      });
      apkJson = response.data;   //json格式{"version":"3.0.1","outputFile":"http://192.168.200.201/test/news.apk","versionName":"ydhl"}
    }
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    //如果你设置的版本号 于你本机上的App的版本号 不一致 弹出下载框
    if (apkJson["version"] != "packageInfo.version") {
      print(packageInfo.version);
      Msg.dlg(context: context,title: "版本不是最新版,请确定是否更新").then((value)=>{
        if(value == ""){
          _installApk(apkJson["outputFile"])
          // download2()
        }
      });
    }else{
      toast("已是最新版");
    }
  }
Dart 复制代码
/// 下载安卓更新包
  Future downloadAndroid(String url) async {
    /// 创建存储文件
    Directory storageDir = await getExternalStorageDirectory();
    String storagePath = storageDir.path;
    final directory = await getTemporaryDirectory();
    File file = new File("${storageDir.path}/ydhl.apk");

    if (!file.existsSync()) {
      file.createSync();
    }
    try {
      /// 发起下载请求

      Response response = await Dio().download(url,file.path,
          options: Options(
            followRedirects: false,
          ),onProgress: (int count, int total){
            if (total != -1) {
              //更新界面进度
              _value = count / total;
              print("------"+_value.toString());
              setState(() {
                _value =_value;
              });
            }
          });
      // file.writeAsBytesSync(response.data);
      return file;
    } catch (e) {
      showError("下载失败");
      debugPrint(e.toString());
      setState(() {

      });
    }
  }
Dart 复制代码
/// 安装apk
  Future _installApk(String url) async {
    File _apkFile = await downloadAndroid(url);
    String _apkFilePath = _apkFile.path;

    if (_apkFilePath.isEmpty) {
      print("make sure the apk file is set");
      return;
    }
    AppInstaller.installApk(_apkFilePath)
        .then((ell){
      toast("下载成功");
    }).catchError((e){
      showError("install apk error: $e");
    });
  }

更新界面

与界面都在stack中

Dart 复制代码
Visibility(
          visible: (_value*100).toInt() != 0 && (_value*100).toInt() != 100,
          child: Stack(
            children: [
              Container(
                height: double.infinity,
                width: double.infinity,
                color: Colors.black12,
              ),
              Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      child: Text("下载中...",style: TextStyle(
                        color: Colors.blue,
                          decoration: TextDecoration.none
                      ),),
                    ),
                    SizedBox(height: 10,),
                    SizedBox(
                      height: 20,
                      width: 300,
                      child: ClipRRect(
                        borderRadius: const BorderRadius.all(Radius.circular(100)),
                        child: LinearProgressIndicator(
                          value: _value,
                          backgroundColor: const Color(0xFF7E7F88).withOpacity(0.15),
                          valueColor: const AlwaysStoppedAnimation<Color>(Colors.green),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
相关推荐
爱吃桃子的ICer44 分钟前
[UVM]3.核心基类 uvm_object 域的自动化 copy() compare() print() pack unpack
开发语言·前端·ic设计
学地理的小胖砸3 小时前
【GEE的Python API】
大数据·开发语言·前端·python·遥感·地图学·地理信息科学
垦利不3 小时前
css总结
前端·css·html
八月的雨季 最後的冰吻4 小时前
C--字符串函数处理总结
c语言·前端·算法
6230_4 小时前
关于HTTP通讯流程知识点补充—常见状态码及常见请求方式
前端·javascript·网络·网络协议·学习·http·html
pan_junbiao5 小时前
Vue组件:使用$emit()方法监听子组件事件
前端·javascript·vue.js
正在绘制中6 小时前
如何部署Vue+Springboot项目
前端·vue.js·spring boot
Keep striving6 小时前
SpringMVC基于注解使用:国际化
java·前端·spring·servlet·tomcat·maven
Loong_DQX6 小时前
【前端】vue+html+js 实现table表格展示,以及分页按钮添加
前端·javascript·vue.js
Boyi美业7 小时前
连锁美业门店开设不同的课程有什么用?美业系统源码分享
java·前端·团队开发·创业创新·源代码管理