import 'dart:io'; import 'package:ctjt_flutter/common/states.dart'; import 'package:ctjt_flutter/model/version_res.dart'; import 'package:ctjt_flutter/service/base_service.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; import 'package:provider/provider.dart'; class AppService extends BaseService { late BuildContext mContext; AppService._(); static final _instance = AppService._(); factory AppService.getInstance(BuildContext context) { _instance.mContext = context; return _instance; } /// 拉取版本号信息 Future fetchVersionInfo(String v) async { int channelType = HTTPConfig.getChannelType(); // 根据环境判断更新渠道类型 var uri = '/common/getAppVersion?currentVersion=' + v + '&channelType=' + channelType.toString(); var data = await request(uri); if (null == data) { return null; } var ver = VersionRes.fromJson(data); print("Got version from remote:" + ver.body!.lastVersion!); return ver.body; } /// 下载安卓更新包 Future downloadAndroid(String url, String version) async { /// 创建存储文件 Directory? storageDir = await getExternalStorageDirectory(); if (null == storageDir) { return null; } String storagePath = storageDir.path; File file = new File('$storagePath/ctjt_flutter_install_$version.apk'); if (!file.existsSync()) { file.createSync(); } /// 发起下载请求 Response? response = await requestFile(url, onReceiveProgress: showDownloadProgress); if (null == response) { return null; } file.writeAsBytesSync(response.data); return file; } /// 展示下载进度 void showDownloadProgress(num received, num total) { if (total != -1) { double _progress = double.parse('${(received / total).toStringAsFixed(2)}'); print('Download process is $_progress'); Provider.of(mContext, listen: false).process = _progress; } } }