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) { MyNavigator.showDialog( builder: (_) => UpgradeDialog(path), tag: 'upgrade', clickDismiss: false, ); } final String path; final String versionName; UpgradeDialog( this.path, { super.key, }) : versionName = path.split('_')[path.split('_').length - 2]; final progress = (-1.0).notifier(); 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( mainAxisSize: MainAxisSize.min, children: [ Container( alignment: Alignment.topLeft, child: Text( '发现新版本 $versionName', style: const TextStyle(color: Color(0xFF333333), fontSize: 16, fontWeight: FontWeight.w500), ), ), // Row( // children: [ // const Spacer(), // progress.builder((v) => v == -1 // ? GestureDetector( // onTap: dismiss, // child: const Text( // '取消', // style: TextStyle(color: Color(0xFF333333), fontSize: 14), // ), // ) // : const SizedBox.shrink()), // ], // ), 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), ), ) ], ); } }