upgrade_dialog.dart 3.4 KB

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