flutter 内网app内部下载

想好逻辑,先将apk下载到本地,再到本地去寻找apk下载

正在下载样式

Visibility(
          visible: (_value*100).toInt() != 0 && (_value*100).toInt() != 100,
          child: Stack(
            children: [
              Expanded(
                child: Container(
                  color: Colors.black12,
                ),
              ),
              Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      child: Text("正在下载中...",style: TextStyle(
                          decoration: TextDecoration.none
                      ),),
                    ),
                    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),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),

下载功能前置条件

插件app_installer  package_info  path_provider  dio

1.允许下载权限在AndroidManifest.xml添加

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 这个权限用于app安装 -->
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- Provider -->
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

android/app/src/main/res/xml/file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="Android/data/packagename/" name="files_root" />
    <external-path path="." name="external_storage_root" />
</paths>

//替换 packagename 为你的包名

下载功能

//查询是否更新
  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;
    }
    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("已是最新版");
    }
  }

  /// 下载安卓更新包
  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(() {

      });
    }
  }

  /// 安装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");
    });
  }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值