flutter_bugly 0.2.0 flutter_bugly: ^0.2.0 copied to clipboard
Flutter plugin for Tencent Bugly, Crash monitoring, Crash analysis, exception reporting, application update, data statistics, etc
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());
});
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget{
@override
State<StatefulWidget> createState() =>_HomePageState();
}
class _HomePageState extends State<HomePage>{
String _platformVersion = 'Unknown';
GlobalKey<UpdateDialogState> _dialogKey = new GlobalKey();
@override
void initState() {
super.initState();
FlutterBugly.init(
androidAppId: "your android id",//测试 5346b6e5bb
iOSAppId: "your app id",
).then((_result) {
setState(() {
_platformVersion = _result.message;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: GestureDetector(
onTap: () {
if (Platform.isAndroid) {
FlutterBugly.getUpgradeInfo().then((UpgradeInfo info) {
print("----------------${info.apkUrl}");
if (info != null && info.id != null) {
showUpdateDialog(
info.newFeature, info.apkUrl, info.upgradeType == 2);
}
});
}
},
child: Center(
child: Text('init result: $_platformVersion\n'),
),
),
);
}
void showUpdateDialog(String version, String url, bool isForceUpgrade) {
showDialog(
barrierDismissible: false,
context: context,
builder: (_) => _buildDialog(version, url, isForceUpgrade),
);
}
Widget _buildDialog(String version, String url, bool isForceUpgrade) {
return WillPopScope(
onWillPop: () async => isForceUpgrade,
child: UpdateDialog(
key: _dialogKey,
version: version,
onClickWhenDownload: (_msg) {
//提示不要重复下载
},
onClickWhenNotDownload: () {
//下载apk,完成后打开apk文件,建议使用dio+open_file插件
},
));
}
//dio可以监听下载进度,调用此方法
void _updateProgress(_progress) {
setState(() {
_dialogKey.currentState.progress = _progress;
});
}
}