1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import 'dart:async';
import 'dart:io';
import 'package:apk_update/utils/image_utils.dart';
import 'package:apk_update/utils/utils.dart';
import 'package:dio/dio.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
import '../apk_update_platform_interface.dart';
class UpdateDialog extends StatefulWidget {
final String? title; // 升级版本
final String? content; // 升级内容
final bool isUpdateMore; // 是否强升
final String? versionPath; // apk 路径
final Function()? jumpAppStore; // 跳转AppStore
final Function(String? path)? installApk; // 安装Apk
final int? isOssDownload; //1 使用OSS下载
final Function(String? path, String? apkPath)? downloadApk; // 使用OSS下载Apk
final Function()? downloadApkError; // 下载Apk错误
const UpdateDialog({
Key? key,
this.title,
this.content,
this.isUpdateMore = false,
this.versionPath,
this.jumpAppStore,
this.installApk,
this.downloadApk,
this.downloadApkError,
this.isOssDownload,
}) : super(key: key);
@override
State<UpdateDialog> createState() => _UpdateDialogState();
}
class _UpdateDialogState extends State<UpdateDialog> {
final CancelToken _cancelToken = CancelToken();
StreamSubscription<Map<String, Object>>? _subscription;
bool _isDownload = false;
double _value = 0;
@override
void dispose() {
if (!_cancelToken.isCancelled && _value != 1) {
_cancelToken.cancel();
}
_subscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () => Future.value(false), //使用false禁止返回键返回,达到强制升级目的
child: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.transparent,
body: Center(
child: SizedBox(
width: 280.0,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
// 顶部图片
_topImage(),
Container(
padding: const EdgeInsets.symmetric(horizontal: 15.0),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(8.0),
bottomRight: Radius.circular(8.0),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 标题
_content(title: widget.title, content: widget.content),
_isDownload
// 下载进度
? LinearProgressIndicator(
backgroundColor: const Color(0xFFF2F3F3),
value: _value,
)
// 按钮
: Column(
children: <Widget>[
_btn(
title: '立即更新',
onTap: () {
if (Platform.isIOS) {
Navigator.pop(context);
widget.jumpAppStore?.call();
} else {
setState(() {
_isDownload = true;
});
_download();
}
},
),
const SizedBox(height: 5.0),
Offstage(
offstage: widget.isUpdateMore,
child: _btn(
title: '忽略此版本',
textColor: const Color(0xFF666666),
bgColor: Colors.transparent,
onTap: () => Navigator.pop(context),
),
),
],
),
const SizedBox(height: 10.0),
],
),
),
],
),
),
),
),
);
}
/// 顶部图片
Widget _topImage() => Container(
height: 140.0,
width: 280.0,
decoration: BoxDecoration(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
image: DecorationImage(
image: ImageUtils.getAssetImage('update_head'),
fit: BoxFit.cover,
),
),
);
/// 标题、内容
Widget _content({String? title, String? content}) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 15.0),
// 标题
Text(
title ?? "",
style: const TextStyle(
fontSize: 15.0,
color: Color(0xFF333C4C),
),
),
const SizedBox(height: 5.0),
// 内容
htmlWidget(content),
const SizedBox(height: 15.0),
],
);
/// 显示html内容
Widget htmlWidget(String? content) {
return HtmlWidget(
widget.content ?? "",
onTapUrl: (url) {
if (Platform.isIOS) {
widget.jumpAppStore?.call();
} else {
openBrowser(url);
}
return true;
},
);
}
/// 按钮
Widget _btn({
String? title,
GestureTapCallback? onTap,
Color? textColor,
Color? bgColor,
}) =>
InkWell(
onTap: onTap,
child: Container(
width: double.infinity,
constraints: const BoxConstraints(minHeight: 40.0),
alignment: Alignment.center,
decoration: BoxDecoration(
color: bgColor ?? Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(4.0),
),
child: Text(
title ?? "",
style: TextStyle(fontSize: 15.0, color: textColor ?? Colors.white),
),
),
);
///下载apk
Future<void> _download() async {
try {
// 2024-11-21 使用OSSSDK下载
if (widget.isOssDownload == 1) {
setInitDir(initStorageDir: true);
await DirectoryUtil.getInstance();
DirectoryUtil.createStorageDirSync(category: 'Download');
String? path = DirectoryUtil.getStoragePath(category: 'Download');
File apkFile = File("$path/${widget.versionPath}");
File apkFileTmp = File("$path/${widget.versionPath}.tmp");
// 判断localPath 下有没有下载完成的文件
if (apkFile.existsSync() && !apkFileTmp.existsSync()) {
ApkUpdatePlatform.instance.installApk(apkFile.path);
_isDownload = false;
setState(() {});
return;
}
_subscription =
ApkUpdatePlatform.instance.addDownloadListener().listen((event) {
var count = event['currentSize'] as num;
var total = event['totalSize'] as num;
var isDownloadSuccess = event['isDownloadSuccess'] == "1";
var isDownloadFailed = event['isDownloadSuccess'] == "0";
if (isDownloadFailed) {
_isDownload = false;
setState(() {});
return;
}
_value = count / total;
if (isDownloadSuccess) {
_isDownload = false;
}
setState(() {});
});
widget.downloadApk?.call(path, widget.versionPath);
return;
}
setInitDir(initStorageDir: true);
await DirectoryUtil.getInstance();
DirectoryUtil.createStorageDirSync(category: 'Download');
String path = DirectoryUtil.getStoragePath(
fileName: 'clx_update', category: 'Download', format: 'apk')!;
File file = File(path);
debugPrint("===== Apk下载路径:$path");
/// 链接可能会失效
await Dio().download(
widget.versionPath!,
file.path,
cancelToken: _cancelToken,
onReceiveProgress: (int count, int total) {
if (total != -1) {
_value = count / total;
setState(() {});
if (count == total) {
debugPrint("===== Apk下载完成");
Navigator.pop(context);
widget.installApk?.call(path);
}
}
},
);
} catch (e) {
widget.downloadApkError?.call();
debugPrint("===== Apk下载错误: $e");
setState(() {
_isDownload = false;
});
}
}
}