upgrade_dialog.dart 3.6 KB

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