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
import 'dart:io';
import 'package:apk_update/utils/image_utils.dart';
import 'package:dio/dio.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.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 Function()? downloadApkError; // 下载Apk错误
const UpdateDialog({
Key? key,
this.title,
this.content,
this.isUpdateMore = false,
this.versionPath,
this.jumpAppStore,
this.installApk,
this.downloadApkError,
}) : super(key: key);
@override
State<UpdateDialog> createState() => _UpdateDialogState();
}
class _UpdateDialogState extends State<UpdateDialog> {
final CancelToken _cancelToken = CancelToken();
bool _isDownload = false;
double _value = 0;
@override
void dispose() {
if (!_cancelToken.isCancelled && _value != 1) {
_cancelToken.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: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8.0),
),
width: 280.0,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
// 顶部图片
_topImage(),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 标题
_content(title: widget.title, content: widget.content),
_isDownload
// 下载进度
? LinearProgressIndicator(
backgroundColor: const Color(0xFFF2F3F3),
value: _value,
)
// 按钮
: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Offstage(
offstage: widget.isUpdateMore,
child: _btn(
title: '残忍拒绝',
textColor: Theme.of(context).primaryColor,
bgColor: Colors.transparent,
onTap: () => Navigator.pop(context),
),
),
_btn(
title: '立即更新',
onTap: () {
if (Platform.isIOS) {
Navigator.pop(context);
widget.jumpAppStore?.call();
} else {
setState(() {
_isDownload = true;
});
_download();
}
},
)
],
),
const SizedBox(height: 15.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(
children: [
const SizedBox(height: 15.0),
// 标题
Text(
title ?? "",
style: const TextStyle(
fontSize: 15.0,
color: Color(0xFF333C4C),
),
),
const SizedBox(height: 5.0),
// 内容
Text(
content ?? "",
style: const TextStyle(fontSize: 15.0, color: Color(0xFF4E5969)),
),
const SizedBox(height: 15.0),
],
);
/// 按钮
Widget _btn({
String? title,
GestureTapCallback? onTap,
Color? textColor,
Color? bgColor,
}) =>
InkWell(
onTap: onTap,
child: Container(
width: 110.0,
constraints: const BoxConstraints(minHeight: 36.0),
alignment: Alignment.center,
decoration: BoxDecoration(
color: bgColor ?? Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(20.0),
border: Border.all(color: Theme.of(context).primaryColor),
),
child: Text(
title ?? "",
style: TextStyle(fontSize: 15.0, color: textColor ?? Colors.white),
),
),
);
///下载apk
Future<void> _download() async {
try {
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;
});
}
}
}