Skip to content

Commit

Permalink
添加自定义更新弹窗例子
Browse files Browse the repository at this point in the history
  • Loading branch information
crazecoder committed Feb 27, 2019
1 parent 5afcf78 commit 6052890
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
28 changes: 26 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_bugly/flutter_bugly.dart';

import 'update_dialog.dart';

void main()=>FlutterBugly.postCatchedException((){
runApp(MyApp());
});
Expand All @@ -14,6 +16,7 @@ class MyApp extends StatefulWidget {

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

@override
void initState() {
Expand All @@ -39,8 +42,10 @@ class _MyAppState extends State<MyApp> {
onTap: () {
if (Platform.isAndroid) {
FlutterBugly.checkUpgrade();
FlutterBugly.getUpgradeInfo().then((_info) {
print("------------------${_info?.title}");
FlutterBugly.getUpgradeInfo().then((UpgradeInfo info) {
if (info != null && info.id != null) {
showUpdateDialog(info.newFeature, info.apkUrl);
}
});
}
},
Expand All @@ -51,4 +56,23 @@ class _MyAppState extends State<MyApp> {
),
);
}
void showUpdateDialog(String version, String url) async {
await showDialog(
barrierDismissible: false,
context: context,
builder: (_) => _buildDialog(version, url),
);
}
Widget _buildDialog(String version, String url) {
return new UpdateDialog(
key:_dialogKey,
version:version,
onClickWhenDownload:(_msg) {
//提示不要重复下载
},
onClickWhenNotDownload:() {
//下载apk,完成后打开apk文件,建议使用dio+open_file插件
},
);
}
}
75 changes: 75 additions & 0 deletions example/lib/update_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'package:flutter/material.dart';

class UpdateDialog extends StatefulWidget {
final key;
final version;
final Function onClickWhenDownload;
final Function onClickWhenNotDownload;

UpdateDialog({
this.key,
this.version,
this.onClickWhenDownload,
this.onClickWhenNotDownload,
});

@override
State<StatefulWidget> createState() => new UpdateDialogState();
}

class UpdateDialogState extends State<UpdateDialog> {
var _downloadProgress = 0.0;

@override
Widget build(BuildContext context) {
var _textStyle =
new TextStyle(color: Theme.of(context).textTheme.body1.color);

return new AlertDialog(
title: new Text(
"有新的更新",
style: _textStyle,
),
content: _downloadProgress == 0.0
? new Text(
"版本${widget.version}",
style: _textStyle,
)
: new LinearProgressIndicator(
value: _downloadProgress,
),
actions: <Widget>[
new FlatButton(
child: new Text(
'更新',
style: _textStyle,
),
onPressed: () {
if (_downloadProgress != 0.0) {
widget.onClickWhenDownload("正在更新中");
return;
}
widget.onClickWhenNotDownload();
// Navigator.of(context).pop();
},
),
new FlatButton(
child: new Text('取消'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
}

set progress(_progress) {
setState(() {
_downloadProgress = _progress;
if (_downloadProgress == 1) {
Navigator.of(context).pop();
_downloadProgress = 0.0;
}
});
}
}

0 comments on commit 6052890

Please sign in to comment.