import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:package_info/package_info.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:url_launcher/url_launcher.dart';
class AppUpdatePage extends StatefulWidget {
@override
_AppUpdatePageState createState() => _AppUpdatePageState();
}
class _AppUpdatePageState extends State<AppUpdatePage> {
String _latestVersion = 'Unknown';
String _downloadUrl = '';
String _currentVersion = 'Unknown';
bool _isDownloading = false;
@override
void initState() {
super.initState();
_getLatestVersion();
_requestPermissions();
_initDownloader();
}
// 获取最新版本信息
Future<void> _getLatestVersion() async {
final packageInfo = await PackageInfo.fromPlatform();
setState(() {
_currentVersion = packageInfo.version;
});
// 这里应该是获取服务器最新版本信息的逻辑
// ...
setState(() {
_latestVersion = '服务器最新版本';
_downloadUrl = '下载地址';
});
}
// 请求权限
Future<void> _requestPermissions() async {
// 请求存储权限
await Permission.storage.request();
}
// 初始化下载器
Future<void> _initDownloader() async {
Downloader.initialize(
debug: false, // 是否启用Log
callback: (id, status, progress) {
// 下载进度回调
}
);
}
// 下载apk
Future<void> _downloadApk() async {
final dir = await getExternalStorageDirectory();
final savedDir = '$dir/.downloads';
try {
setState(() {
_isDownloading = true;
});
int result = await Downloader.enqueue(
url: _downloadUrl,
savedDir: savedDir,
fileName: 'update.apk',
showNotification: true, // 是否显示下载通知
openNotification: true, // 是否自动打开下载完成的通知
);
print('下载任务ID: $r
评论已关闭