upgrade_dialog.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Container(
  73. alignment: Alignment.topLeft,
  74. child: Text(
  75. '发现新版本 $versionName',
  76. style: const TextStyle(color: Color(0xFF333333), fontSize: 16, fontWeight: FontWeight.w500),
  77. ),
  78. ),
  79. // Row(
  80. // children: [
  81. // const Spacer(),
  82. // progress.builder((v) => v == -1
  83. // ? GestureDetector(
  84. // onTap: dismiss,
  85. // child: const Text(
  86. // '取消',
  87. // style: TextStyle(color: Color(0xFF333333), fontSize: 14),
  88. // ),
  89. // )
  90. // : const SizedBox.shrink()),
  91. // ],
  92. // ),
  93. const SizedBox(height: 24),
  94. progress.builder((v) => v == -1 ? MyButton('立即下载安装', onTap: onDownload, alignment: null) : buildProgress(v)),
  95. ],
  96. ),
  97. );
  98. }
  99. Widget buildProgress(double value) {
  100. int pre = (double.parse(value.toStringAsFixed(2)) * 100).toInt();
  101. return Row(
  102. children: [
  103. Expanded(flex: 6, child: LinearProgressIndicator(value: value, borderRadius: BorderRadius.circular(8), minHeight: 8)),
  104. Expanded(
  105. child: Text(
  106. '$pre%',
  107. textAlign: TextAlign.right,
  108. style: const TextStyle(color: Color(0xFF333333), fontSize: 14),
  109. ),
  110. )
  111. ],
  112. );
  113. }
  114. }