update_dialog.dart
6.2 KB
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
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:one_poem/res/resources.dart';
import 'package:one_poem/util/other_utils.dart';
import 'package:one_poem/routers/fluro_navigator.dart';
import 'package:one_poem/util/image_utils.dart';
import 'package:one_poem/util/theme_utils.dart';
import 'package:one_poem/util/toast_utils.dart';
import 'package:one_poem/util/version_utils.dart';
import 'package:one_poem/widgets/my_button.dart';
import 'package:one_poem/extension/int_extension.dart';
class UpdateDialog extends StatefulWidget {
const UpdateDialog({Key? key}) : super(key: key);
@override
_UpdateDialogState 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) {
final Color primaryColor = Theme.of(context).primaryColor;
return WillPopScope(
onWillPop: () async {
/// 使用false禁止返回键返回,达到强制升级目的
return true;
},
child: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.transparent,
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
height: 120.px,
width: 280.px,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.px),
topRight: Radius.circular(8.px)),
image: DecorationImage(
image: ImageUtils.getAssetImage('update_head',
format: ImageFormat.jpg),
fit: BoxFit.cover,
),
),
),
Container(
width: 280.px,
decoration: BoxDecoration(
color: context.dialogBackgroundColor,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(8.px),
bottomRight: Radius.circular(8.px),
),
),
padding: EdgeInsets.symmetric(
horizontal: 15.px,
vertical: 15.px,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text('新版本更新', style: TextStyles.textSize16),
Gaps.vGap10,
const Text('1.又双叒修复了一大堆bug。\n\n2.祭天了多名程序猿。'),
Gaps.vGap15,
if (_isDownload)
LinearProgressIndicator(
backgroundColor: Colours.line,
valueColor:
AlwaysStoppedAnimation<Color>(primaryColor),
value: _value,
)
else
_buildButton(context),
],
),
),
],
),
)),
);
}
Widget _buildButton(BuildContext context) {
final Color primaryColor = Theme.of(context).primaryColor;
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
SizedBox(
width: 110.px,
height: 36.px,
child: MyButton(
text: '残忍拒绝',
fontSize: Dimens.font_sp16,
textColor: primaryColor,
disabledTextColor: Colors.white,
disabledBackgroundColor: Colours.text_gray_c,
radius: 18.px,
side: BorderSide(
color: primaryColor,
width: 0.8,
),
backgroundColor: Colors.transparent,
onPressed: () {
NavigatorUtils.goBack(context);
},
),
),
SizedBox(
width: 110.px,
height: 36.px,
child: MyButton(
text: '立即更新',
fontSize: Dimens.font_sp16,
onPressed: () {
if (defaultTargetPlatform == TargetPlatform.iOS) {
NavigatorUtils.goBack(context);
VersionUtils.jumpAppStore();
} else {
setState(() {
_isDownload = true;
});
_download();
}
},
textColor: Colors.white,
backgroundColor: primaryColor,
disabledTextColor: Colors.white,
disabledBackgroundColor: Colours.text_gray_c,
radius: 18.px,
),
)
],
);
}
///下载apk
Future<void> _download() async {
try {
setInitDir(initStorageDir: true);
await DirectoryUtil.getInstance();
DirectoryUtil.createStorageDirSync(category: 'Download');
final String path = DirectoryUtil.getStoragePath(
fileName: 'deer', category: 'Download', format: 'apk')
.nullSafe;
final File file = File(path);
/// TODO 需要替换成正式APP地址
await Dio().download(
'http://imtt.dd.qq.com/16891/apk/FF9625F40FD26F015F4CDED37B6B66AE.apk',
file.path,
cancelToken: _cancelToken,
onReceiveProgress: (int count, int total) {
if (total != -1) {
_value = count / total;
setState(() {});
if (count == total) {
NavigatorUtils.goBack(context);
VersionUtils.install(path);
}
}
},
);
} catch (e) {
Toast.show('下载失败!');
debugPrint(e.toString());
setState(() {
_isDownload = false;
});
}
}
}