upgrade_dialog.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'package:flutter/material.dart';
  2. import 'package:lszlgl/main.dart';
  3. import 'package:lszlgl/service/upgrade_service.dart';
  4. import 'package:lszlgl/widget/button.dart';
  5. import 'package:lszlgl/ext/value_notifier_ext.dart';
  6. import '../router/my_navigator.dart';
  7. class UpgradeDialog extends StatelessWidget {
  8. static void showDialog(String path) {
  9. MyNavigator.showDialog(
  10. builder: (_) => UpgradeDialog(path),
  11. tag: 'upgrade',
  12. clickDismiss: false,
  13. );
  14. }
  15. final String path;
  16. final String versionName;
  17. UpgradeDialog(
  18. this.path, {
  19. super.key,
  20. }) : versionName = path.split('_')[path.split('_').length - 2];
  21. final progress = (-1.0).notifier<double>();
  22. void dismiss() {
  23. MyNavigator.dismiss(tag: 'upgrade');
  24. }
  25. void onDownload() async {
  26. UpgradeService.getName(path);
  27. // 拆分文件名
  28. var apkName = UpgradeService.getName(path);
  29. logger.d('path:$path');
  30. logger.d('apkName:$apkName');
  31. var file = await UpgradeService.getApkFile(apkName);
  32. // 已有文件 直接安装
  33. if (await file.exists()) {
  34. installApk(file.path);
  35. return;
  36. }
  37. // 无文件 去下载
  38. try {
  39. await UpgradeService.download(
  40. path,
  41. apkName,
  42. (count, total) => progress.value = count / total,
  43. );
  44. installApk(file.path);
  45. } catch (e) {
  46. logger.e(e);
  47. MyNavigator.showToast('下载失败,请重试');
  48. progress.value = -1;
  49. }
  50. }
  51. /// 安装apk
  52. void installApk(String path) async {
  53. bool success = await UpgradeService.installApk(path);
  54. if (success) {
  55. dismiss();
  56. } else {
  57. progress.value = -1;
  58. }
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. return Container(
  63. margin: const EdgeInsets.symmetric(horizontal: 24),
  64. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
  65. decoration: const BoxDecoration(
  66. color: Colors.white,
  67. borderRadius: BorderRadius.all(Radius.circular(8)),
  68. ),
  69. child: Column(
  70. mainAxisSize: MainAxisSize.min,
  71. children: [
  72. Row(
  73. children: [
  74. Text(
  75. '发现新版本 $versionName',
  76. style: const TextStyle(color: Color(0xFF333333), fontSize: 16, fontWeight: FontWeight.w500),
  77. ),
  78. const Spacer(),
  79. progress.builder((v) => v == -1
  80. ? GestureDetector(
  81. onTap: dismiss,
  82. child: const Text(
  83. '取消',
  84. style: TextStyle(color: Color(0xFF333333), fontSize: 14),
  85. ),
  86. )
  87. : const SizedBox.shrink()),
  88. ],
  89. ),
  90. const SizedBox(height: 24),
  91. progress.builder((v) => v == -1 ? MyButton('立即下载安装', onTap: onDownload, alignment: null) : buildProgress(v)),
  92. ],
  93. ),
  94. );
  95. }
  96. Widget buildProgress(double value) {
  97. int pre = (double.parse(value.toStringAsFixed(2)) * 100).toInt();
  98. return Row(
  99. children: [
  100. Expanded(flex: 6, child: LinearProgressIndicator(value: value, borderRadius: BorderRadius.circular(8), minHeight: 8)),
  101. Expanded(
  102. child: Text(
  103. '$pre%',
  104. textAlign: TextAlign.right,
  105. style: const TextStyle(color: Color(0xFF333333), fontSize: 14),
  106. ),
  107. )
  108. ],
  109. );
  110. }
  111. }