Skip to content

Commit 6052890

Browse files
committed
添加自定义更新弹窗例子
1 parent 5afcf78 commit 6052890

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

example/lib/main.dart

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import 'dart:io';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_bugly/flutter_bugly.dart';
55

6+
import 'update_dialog.dart';
7+
68
void main()=>FlutterBugly.postCatchedException((){
79
runApp(MyApp());
810
});
@@ -14,6 +16,7 @@ class MyApp extends StatefulWidget {
1416

1517
class _MyAppState extends State<MyApp> {
1618
String _platformVersion = 'Unknown';
19+
GlobalKey<UpdateDialogState> _dialogKey = new GlobalKey();
1720

1821
@override
1922
void initState() {
@@ -39,8 +42,10 @@ class _MyAppState extends State<MyApp> {
3942
onTap: () {
4043
if (Platform.isAndroid) {
4144
FlutterBugly.checkUpgrade();
42-
FlutterBugly.getUpgradeInfo().then((_info) {
43-
print("------------------${_info?.title}");
45+
FlutterBugly.getUpgradeInfo().then((UpgradeInfo info) {
46+
if (info != null && info.id != null) {
47+
showUpdateDialog(info.newFeature, info.apkUrl);
48+
}
4449
});
4550
}
4651
},
@@ -51,4 +56,23 @@ class _MyAppState extends State<MyApp> {
5156
),
5257
);
5358
}
59+
void showUpdateDialog(String version, String url) async {
60+
await showDialog(
61+
barrierDismissible: false,
62+
context: context,
63+
builder: (_) => _buildDialog(version, url),
64+
);
65+
}
66+
Widget _buildDialog(String version, String url) {
67+
return new UpdateDialog(
68+
key:_dialogKey,
69+
version:version,
70+
onClickWhenDownload:(_msg) {
71+
//提示不要重复下载
72+
},
73+
onClickWhenNotDownload:() {
74+
//下载apk,完成后打开apk文件,建议使用dio+open_file插件
75+
},
76+
);
77+
}
5478
}

example/lib/update_dialog.dart

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import 'package:flutter/material.dart';
2+
3+
class UpdateDialog extends StatefulWidget {
4+
final key;
5+
final version;
6+
final Function onClickWhenDownload;
7+
final Function onClickWhenNotDownload;
8+
9+
UpdateDialog({
10+
this.key,
11+
this.version,
12+
this.onClickWhenDownload,
13+
this.onClickWhenNotDownload,
14+
});
15+
16+
@override
17+
State<StatefulWidget> createState() => new UpdateDialogState();
18+
}
19+
20+
class UpdateDialogState extends State<UpdateDialog> {
21+
var _downloadProgress = 0.0;
22+
23+
@override
24+
Widget build(BuildContext context) {
25+
var _textStyle =
26+
new TextStyle(color: Theme.of(context).textTheme.body1.color);
27+
28+
return new AlertDialog(
29+
title: new Text(
30+
"有新的更新",
31+
style: _textStyle,
32+
),
33+
content: _downloadProgress == 0.0
34+
? new Text(
35+
"版本${widget.version}",
36+
style: _textStyle,
37+
)
38+
: new LinearProgressIndicator(
39+
value: _downloadProgress,
40+
),
41+
actions: <Widget>[
42+
new FlatButton(
43+
child: new Text(
44+
'更新',
45+
style: _textStyle,
46+
),
47+
onPressed: () {
48+
if (_downloadProgress != 0.0) {
49+
widget.onClickWhenDownload("正在更新中");
50+
return;
51+
}
52+
widget.onClickWhenNotDownload();
53+
// Navigator.of(context).pop();
54+
},
55+
),
56+
new FlatButton(
57+
child: new Text('取消'),
58+
onPressed: () {
59+
Navigator.of(context).pop();
60+
},
61+
),
62+
],
63+
);
64+
}
65+
66+
set progress(_progress) {
67+
setState(() {
68+
_downloadProgress = _progress;
69+
if (_downloadProgress == 1) {
70+
Navigator.of(context).pop();
71+
_downloadProgress = 0.0;
72+
}
73+
});
74+
}
75+
}

0 commit comments

Comments
 (0)