easy_app_installer 0.0.5
easy_app_installer: ^0.0.5 copied to clipboard
It provides easy installation and upgrade for applications, come on, take a look.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:easy_app_installer/easy_app_installer.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _cancelTag = "";
String _apkFilePath = "";
String _currentDownloadStateCH = "当前下载状态:还未开始";
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion = await EasyAppInstaller.instance.platformVersion ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
builder: EasyLoading.init(),
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(_currentDownloadStateCH),
_buildButton('下载并安装apk', () {
downloadAndInstalApk();
}),
_buildButton('取消下载任务', () {
if (_cancelTag.isNotEmpty) {
EasyLoading.dismiss();
EasyAppInstaller.instance.cancelDownload(_cancelTag);
} else {
EasyLoading.showError("没有下载中的任务");
}
}),
_buildButton('仅安装', () async {
final path =
await EasyAppInstaller.instance.installApk(_apkFilePath);
print("gfs installApk: $path");
}),
_buildButton('打开应用市场', () {
EasyAppInstaller.instance.openAppMarket();
}),
_buildButton('打开设置详情页', () {
EasyAppInstaller.instance.openAppSettingDetails();
}),
],
),
),
),
);
}
void downloadAndInstalApk() async {
//fileUrl需替换为指定apk地址
await EasyAppInstaller.instance.downloadAndInstallApk(
fileUrl: "https://hipos.oss-cn-shanghai.aliyuncs.com/hipos-kds-v.5.10.0321.apk",
fileDirectory: "updateApk",
fileName: "newApk.apk",
explainContent: "快去开启权限!!!",
downloadListener: (progress) {
if (progress < 100) {
EasyLoading.showProgress(progress / 100, status: "下载中");
} else {
EasyLoading.showSuccess("下载成功");
}
},
cancelTagListener: (cancelTag) {
_cancelTag = cancelTag;
},
stateListener: (newState, attachParam) {
_handleDownloadStateChanged(newState, attachParam);
});
}
/// 处理下载状态更改
void _handleDownloadStateChanged(
EasyAppInstallerState newState, String? attachParam) {
switch (newState) {
case EasyAppInstallerState.onPrepared:
_currentDownloadStateCH = "当前下载状态:开始下载";
break;
case EasyAppInstallerState.onDownloading:
_currentDownloadStateCH = "当前下载状态:下载中";
break;
case EasyAppInstallerState.onSuccess:
if (attachParam != null) {
_currentDownloadStateCH = "当前下载状态:下载成功, $attachParam";
_apkFilePath = attachParam;
}
break;
case EasyAppInstallerState.onFailed:
_currentDownloadStateCH = "当前下载状态:下载失败, $attachParam";
break;
case EasyAppInstallerState.onCanceled:
_currentDownloadStateCH = "当前下载状态:取消下载";
break;
}
setState(() {});
}
Widget _buildButton(String text, Function function) {
return MaterialButton(
color: Colors.blue,
child: Text(
text,
style: const TextStyle(color: Colors.white),
),
onPressed: () {
function();
},
);
}
@override
void dispose() {
EasyAppInstaller.instance.dispose();
super.dispose();
}
}