123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import 'package:flutter/material.dart';
- import 'package:lszlgl/main.dart';
- import 'package:lszlgl/service/upgrade_service.dart';
- import 'package:lszlgl/widget/button.dart';
- import 'package:lszlgl/ext/value_notifier_ext.dart';
- import '../router/my_navigator.dart';
- class UpgradeDialog extends StatelessWidget {
- static void showDialog(String path,String contents) {
- MyNavigator.showDialog(
- builder: (_) => UpgradeDialog(path,contents),
- tag: 'upgrade',
- clickDismiss: false,
- );
- }
- final String path;
- final String contents;
- final String versionName;
- UpgradeDialog(
- this.path,this.contents, {
- super.key,
- }) : versionName = path.split('_')[path.split('_').length - 2];
- final progress = (-1.0).notifier<double>();
- void dismiss() {
- MyNavigator.dismiss(tag: 'upgrade');
- }
- void onDownload() async {
- UpgradeService.getName(path);
- // 拆分文件名
- var apkName = UpgradeService.getName(path);
- logger.d('path:$path');
- logger.d('apkName:$apkName');
- var file = await UpgradeService.getApkFile(apkName);
- // 已有文件 直接安装
- if (await file.exists()) {
- installApk(file.path);
- return;
- }
- // 无文件 去下载
- try {
- await UpgradeService.download(
- path,
- apkName,
- (count, total) => progress.value = count / total,
- );
- installApk(file.path);
- } catch (e) {
- logger.e(e);
- MyNavigator.showToast('下载失败,请重试');
- progress.value = -1;
- }
- }
- /// 安装apk
- void installApk(String path) async {
- bool success = await UpgradeService.installApk(path);
- if (success) {
- // dismiss();
- } else {
- progress.value = -1;
- }
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- margin: const EdgeInsets.symmetric(horizontal: 24),
- padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
- decoration: const BoxDecoration(
- color: Colors.white,
- borderRadius: BorderRadius.all(Radius.circular(8)),
- ),
- child: Column(
- // crossAxisAlignment: CrossAxisAlignment.stretch,
- mainAxisSize: MainAxisSize.min,
- children: [
- Container(
- alignment: Alignment.topCenter,
- child: Text(
- '发现新版本 $versionName',
- style: const TextStyle(color: Color(0xFF333333), fontSize: 16, fontWeight: FontWeight.bold),
- ),
- ),
- const SizedBox(height: 12),
- const SizedBox(
- width: double.infinity,
- child: Text(
- '更新内容:',
- style: TextStyle(fontSize: 14,color: Color(0xFF333333),fontWeight: FontWeight.bold),
- ),
- ),
- const SizedBox(height: 4),
- SizedBox(
- width: double.infinity,
- child: Text(
- contents,
- style: const TextStyle(fontSize: 14,color: Color(0xFF333333)),
- ),
- ),
- const SizedBox(height: 24),
- progress.builder((v) => v == -1 ? MyButton('立即下载安装', onTap: onDownload, alignment: null) : buildProgress(v)),
- ],
- ),
- );
- }
- Widget buildProgress(double value) {
- int pre = (double.parse(value.toStringAsFixed(2)) * 100).toInt();
- return Row(
- children: [
- Expanded(flex: 6, child: LinearProgressIndicator(value: value, borderRadius: BorderRadius.circular(8), minHeight: 8)),
- Expanded(
- child: Text(
- '$pre%',
- textAlign: TextAlign.right,
- style: const TextStyle(color: Color(0xFF333333), fontSize: 14),
- ),
- )
- ],
- );
- }
- }
|