想好逻辑,先将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");
});
}