Skip to content

Commit c309b11

Browse files
committed
[function] 完成 ota 页面重构
Signed-off-by: xiaweizi <1012126908@qq.com>
1 parent 324e703 commit c309b11

File tree

4 files changed

+385
-36
lines changed

4 files changed

+385
-36
lines changed

lib/app/utils/ota_utils.dart

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'package:package_info/package_info.dart';
1010
import 'package:umeng_analytics_plugin/umeng_analytics_plugin.dart';
1111

1212
class OTAUtils {
13-
static startOTA(String url) async {
13+
static startOTA(String url, void onData(OtaEvent event)) async {
1414
try {
1515
OtaUpdate()
1616
.execute(
@@ -19,18 +19,23 @@ class OTAUtils {
1919
)
2020
.listen(
2121
(OtaEvent event) {
22+
onData(event);
2223
print('status: ${event.status}, value: ${event.value}');
2324
if (event.status == OtaStatus.DOWNLOAD_ERROR) {
2425
ToastUtils.show("下载失败", globalKey.currentContext);
25-
UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "download_error");
26+
UmengAnalyticsPlugin.event(AnalyticsConstant.ota,
27+
label: "download_error");
2628
} else if (event.status == OtaStatus.INTERNAL_ERROR) {
2729
ToastUtils.show("未知失败", globalKey.currentContext);
28-
UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "internal_error");
30+
UmengAnalyticsPlugin.event(AnalyticsConstant.ota,
31+
label: "internal_error");
2932
} else if (event.status == OtaStatus.PERMISSION_NOT_GRANTED_ERROR) {
30-
UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "permission_not_granted_error");
33+
UmengAnalyticsPlugin.event(AnalyticsConstant.ota,
34+
label: "permission_not_granted_error");
3135
ToastUtils.show("请打开权限", globalKey.currentContext);
3236
} else if (event.status == OtaStatus.INSTALLING) {
33-
UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "installing");
37+
UmengAnalyticsPlugin.event(AnalyticsConstant.ota,
38+
label: "installing");
3439
ToastUtils.show("正在安装...", globalKey.currentContext);
3540
}
3641
},
@@ -48,42 +53,13 @@ class OTAUtils {
4853
if (otaData != null && otaData["data"] != null) {
4954
String url = otaData["data"]["url"];
5055
String desc = otaData["data"]["desc"];
56+
String versionName = ""; // todo 添加 versionName 的接口配置
5157
int appCode = int.parse(otaData["data"]["appCode"]);
5258
var packageInfo = await PackageInfo.fromPlatform();
5359
var number = int.parse(packageInfo.buildNumber);
5460
if (appCode > number) {
5561
UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "needOTA");
56-
showDialog(
57-
context: globalKey.currentContext,
58-
barrierDismissible: false,
59-
builder: (BuildContext context) {
60-
String content = "是否更新?";
61-
if (desc != null && desc.isNotEmpty) {
62-
content = desc + "\r\n\r\n是否更新?";
63-
}
64-
return AlertDialog(
65-
title: Text("检测到新版本更新"),
66-
content: Text(content),
67-
actions: [
68-
FlatButton(
69-
child: Text("更新"),
70-
onPressed: () {
71-
UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "start");
72-
startOTA(url);
73-
Navigator.of(globalKey.currentContext).pop();
74-
ToastUtils.show("正在后台下载中...", context);
75-
},
76-
),
77-
FlatButton(
78-
child: Text("取消"),
79-
onPressed: () {
80-
UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "cancel");
81-
Navigator.of(globalKey.currentContext).pop();
82-
},
83-
),
84-
],
85-
);
86-
});
62+
showOTADialog(url, desc, versionName);
8763
}
8864
}
8965
}

lib/views/app/flutter_app.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter_dynamic_weather/app/res/weather_type.dart';
55
import 'package:flutter_dynamic_weather/app/router.dart';
66
import 'package:flutter_dynamic_weather/app/utils/image_utils.dart';
77
import 'package:flutter_dynamic_weather/views/common/loading_dialog.dart';
8+
import 'package:flutter_dynamic_weather/views/common/ota_dialog.dart';
89
import 'package:flutter_dynamic_weather/views/pages/home/home_page.dart';
910
import 'dart:ui' as ui;
1011

@@ -26,6 +27,15 @@ void showAppDialog({String loadingMsg = "正在加载中..."}) {
2627
});
2728
}
2829

30+
void showOTADialog(String apkUrl, String desc, String versionName) {
31+
showDialog<LoadingDialog>(
32+
context: globalKey.currentContext,
33+
barrierDismissible: false,
34+
builder: (BuildContext context) {
35+
return new OTADialog(desc, versionName, apkUrl);
36+
});
37+
}
38+
2939
void fetchWeatherImages() async {
3040
WeatherType.values.forEach((element) async {
3141
weatherImages[element] = await ImageUtils.getImage(WeatherUtils.getWeatherIcon(element));

lib/views/common/ota_dialog.dart

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
import 'dart:math';
2+
import 'dart:ui';
3+
4+
import 'package:flutter/material.dart';
5+
import 'package:flutter_dynamic_weather/app/res/analytics_constant.dart';
6+
import 'package:flutter_dynamic_weather/app/utils/ota_utils.dart';
7+
import 'package:flutter_dynamic_weather/app/utils/print_utils.dart';
8+
import 'package:flutter_dynamic_weather/views/common/wave_view.dart';
9+
import 'package:flutter_weather_bg/bg/weather_bg.dart';
10+
import 'package:flutter_weather_bg/flutter_weather_bg.dart';
11+
import 'package:ota_update/ota_update.dart';
12+
import 'package:umeng_analytics_plugin/umeng_analytics_plugin.dart';
13+
14+
var radius = 20.0;
15+
var width = 250.0;
16+
var height = 350.0;
17+
var weatherType = WeatherType.sunny;
18+
19+
class OTADialog extends Dialog {
20+
final desc;
21+
final versionName;
22+
final apkUrl;
23+
24+
OTADialog(this.desc, this.versionName, this.apkUrl, {Key key})
25+
: super(key: key);
26+
27+
@override
28+
Widget build(BuildContext context) {
29+
weatherType =
30+
WeatherType.values[Random().nextInt(WeatherType.values.length)];
31+
return new Material(
32+
type: MaterialType.transparency,
33+
child: new Center(
34+
child: new SizedBox(
35+
width: width,
36+
height: height,
37+
child: ClipPath(
38+
clipper: ShapeBorderClipper(
39+
shape: RoundedRectangleBorder(
40+
borderRadius: BorderRadius.circular(radius))),
41+
child: Stack(
42+
children: [
43+
WeatherBg(
44+
weatherType: weatherType,
45+
width: width,
46+
height: height,
47+
),
48+
BackdropFilter(
49+
filter: ImageFilter.blur(
50+
sigmaX: 1,
51+
sigmaY: 1,
52+
),
53+
child: Container(
54+
width: width,
55+
height: height,
56+
color: Colors.white.withAlpha(60),
57+
),
58+
),
59+
OTAContentWidget(desc, versionName, apkUrl),
60+
],
61+
),
62+
),
63+
),
64+
),
65+
);
66+
}
67+
}
68+
69+
class OTAContentWidget extends StatefulWidget {
70+
71+
final desc;
72+
final versionName;
73+
final apkUrl;
74+
75+
OTAContentWidget(this.desc, this.versionName, this.apkUrl, {Key key})
76+
: super(key: key);
77+
78+
@override
79+
_OTAContentWidgetState createState() => _OTAContentWidgetState();
80+
}
81+
82+
class _OTAContentWidgetState extends State<OTAContentWidget> {
83+
double progress = 0.0;
84+
OtaStatus otaStatus;
85+
86+
Widget _buildWaveBg() {
87+
weatherPrint("_buildWaveBg progress: $progress, status: $otaStatus");
88+
if (otaStatus == OtaStatus.DOWNLOADING) {
89+
return WaveProgress(
90+
Size(width, height), WeatherUtil.getColor(weatherType)[0], progress);
91+
}
92+
return Container();
93+
}
94+
95+
Widget _buildUpdateButton() {
96+
String buttonText = "立即更新";
97+
VoidCallback action = startOta;
98+
if (otaStatus == OtaStatus.DOWNLOADING) {
99+
buttonText = "正在下载...";
100+
action = null;
101+
} else if (otaStatus == OtaStatus.PERMISSION_NOT_GRANTED_ERROR) {
102+
buttonText = "请打开权限";
103+
action = null;
104+
}
105+
return Container(
106+
margin: EdgeInsets.only(left: 40, right: 40, bottom: 25, top: 10),
107+
width: width,
108+
child: MaterialButton(
109+
onPressed: action,
110+
disabledColor: WeatherUtil.getColor(weatherType)[1].withOpacity(0.5),
111+
shape: RoundedRectangleBorder(
112+
side: BorderSide.none,
113+
borderRadius: BorderRadius.circular(25),
114+
),
115+
child: Text(
116+
buttonText,
117+
style: TextStyle(
118+
color: Colors.white, fontSize: 13, fontWeight: FontWeight.w500),
119+
),
120+
color: WeatherUtil.getColor(weatherType)[1],
121+
),
122+
);
123+
}
124+
125+
void startOta() async {
126+
UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "start");
127+
OTAUtils.startOTA(
128+
widget.apkUrl,
129+
(event) {
130+
otaStatus = event.status;
131+
if (event.status == OtaStatus.DOWNLOADING) {
132+
progress = int.parse(event.value).toDouble();
133+
} else if (event.status == OtaStatus.INSTALLING) {
134+
Navigator.of(context).pop();
135+
}
136+
setState(() {});
137+
});
138+
}
139+
140+
@override
141+
Widget build(BuildContext context) {
142+
return Container(
143+
width: width,
144+
height: height,
145+
child: Stack(
146+
children: [
147+
_buildWaveBg(),
148+
Positioned(
149+
right: 10,
150+
top: 10,
151+
child: IconButton(
152+
icon: Icon(
153+
Icons.close,
154+
color: Colors.white.withAlpha(188),
155+
),
156+
onPressed: () {
157+
UmengAnalyticsPlugin.event(AnalyticsConstant.ota,
158+
label: "cancel");
159+
Navigator.of(context).pop();
160+
},
161+
),
162+
),
163+
Column(
164+
crossAxisAlignment: CrossAxisAlignment.start,
165+
children: [
166+
Container(
167+
margin: EdgeInsets.only(top: 20, left: 30),
168+
child: Row(
169+
crossAxisAlignment: CrossAxisAlignment.end,
170+
children: [
171+
Text(
172+
"版本更新",
173+
style: TextStyle(
174+
color: Colors.white,
175+
fontStyle: FontStyle.italic,
176+
fontWeight: FontWeight.bold,
177+
fontSize: 20,
178+
shadows: [
179+
Shadow(
180+
color: Colors.black.withAlpha(100),
181+
offset: Offset(6, 3),
182+
blurRadius: 4)
183+
],
184+
),
185+
),
186+
SizedBox(
187+
width: 20,
188+
),
189+
Text(
190+
widget.versionName,
191+
style: TextStyle(
192+
color: Colors.white,
193+
fontStyle: FontStyle.italic,
194+
fontWeight: FontWeight.bold,
195+
fontSize: 15,
196+
shadows: [
197+
Shadow(
198+
color: Colors.black.withAlpha(80),
199+
offset: Offset(2, 1),
200+
blurRadius: 4)
201+
],
202+
),
203+
)
204+
],
205+
),
206+
),
207+
Container(
208+
margin: EdgeInsets.only(left: 15, top: 30),
209+
child: Text(
210+
"更新内容",
211+
style: TextStyle(
212+
fontWeight: FontWeight.bold,
213+
fontSize: 15,
214+
color: Colors.white),
215+
),
216+
),
217+
Expanded(
218+
child: SingleChildScrollView(
219+
physics: BouncingScrollPhysics(),
220+
child: Container(
221+
margin: EdgeInsets.only(left: 15, top: 15, right: 12),
222+
child: Text(
223+
widget.desc,
224+
style: TextStyle(color: Colors.white, height: 1.8),
225+
),
226+
),
227+
),
228+
),
229+
_buildUpdateButton(),
230+
],
231+
),
232+
],
233+
),
234+
);
235+
}
236+
}

0 commit comments

Comments
 (0)