| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:flutter/widgets.dart';
- import 'package:flutter_bugly/flutter_bugly.dart';
- import 'package:intl/intl.dart';
- import 'package:dio/dio.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:open_file/open_file.dart';
- import 'package:permission_handler/permission_handler.dart';
- Future checkUpgrade(context, key) async {
- final upgradeInfo = await FlutterBugly.getUpgradeInfo();
- if (upgradeInfo == null) {
- return false;
- }
- return await showUpgradeDailog(context, upgradeInfo, key);
- }
- Future showUpgradeDailog(context, upgradeInfo, key) async {
- return await showGeneralDialog(
- context: context,
- barrierDismissible: false,
- pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
- return WillPopScope(
- onWillPop: () {
- return Future.value(false);
- },
- child: Center(
- child: Material(
- color: Colors.transparent,
- child: UpgradeDialog(upgradeInfo, key: key),
- ),
- ),
- );
- },
- barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
- barrierColor: Colors.black26,
- transitionDuration: const Duration(milliseconds: 300),
- );
- }
- Future downloadApk(context, url) async {
- return await showGeneralDialog(
- context: context,
- barrierDismissible: false,
- pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
- return Center(
- child: Material(
- color: Colors.transparent,
- child: DownloadDialog(url),
- ),
- );
- },
- barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
- barrierColor: Colors.black26,
- transitionDuration: const Duration(milliseconds: 300),
- );
- }
- class UpgradeDialog extends StatefulWidget {
- final UpgradeInfo upgradeInfo;
- UpgradeDialog(this.upgradeInfo, {Key key}) : super(key: key);
- @override
- State<StatefulWidget> createState() {
- return UpgradeDialogState();
- }
- }
- class UpgradeDialogState extends State<UpgradeDialog> {
- final Color primaryColor = Color(0xFF1990F8);
- final Color bgColor = Color(0xE6293559);
- @override
- Widget build(BuildContext context) {
- return Container(
- width: 280,
- padding: EdgeInsets.only(left: 20, right: 20),
- decoration: BoxDecoration(color: bgColor, border: Border.all(width: 1, color: primaryColor)),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.stretch,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- Container(
- margin: EdgeInsets.only(top: 20),
- child: Text(
- widget.upgradeInfo.title,
- style: TextStyle(color: primaryColor, fontSize: 16, fontWeight: FontWeight.bold),
- textAlign: TextAlign.center,
- ),
- ),
- Container(
- margin: EdgeInsets.only(top: 15),
- child: Text(
- '版本:' + widget.upgradeInfo.versionName,
- style: TextStyle(color: Colors.white),
- ),
- ),
- Container(
- margin: EdgeInsets.only(top: 10),
- child: Text(
- '包大小:' + (widget.upgradeInfo.fileSize / 1024 / 1024).toStringAsFixed(0) + 'MB',
- style: TextStyle(color: Colors.white),
- ),
- ),
- Container(
- margin: EdgeInsets.only(top: 10),
- child: Text(
- '更新时间:' + DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.fromMicrosecondsSinceEpoch(widget.upgradeInfo.publishTime * 1000)),
- style: TextStyle(color: Colors.white),
- ),
- ),
- Container(
- margin: EdgeInsets.only(top: 10),
- child: Text(
- '更新说明:',
- style: TextStyle(color: Colors.white),
- ),
- ),
- Container(
- margin: EdgeInsets.only(top: 6),
- child: Text(
- widget.upgradeInfo.newFeature,
- style: TextStyle(color: Colors.white),
- ),
- ),
- Container(
- margin: EdgeInsets.only(bottom: 20, top: 20),
- child: Row(
- children: <Widget>[
- widget.upgradeInfo.upgradeType == 2
- ? Container()
- : Expanded(
- child: MaterialButton(
- elevation: 0,
- highlightElevation: 0,
- minWidth: 100,
- height: 36,
- color: Color(0xFF4F5C87),
- child: Text(
- '下次再说',
- style: TextStyle(color: Colors.white),
- ),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- ),
- widget.upgradeInfo.upgradeType == 2 ? Container() : Container(width: 20),
- Expanded(
- child: MaterialButton(
- elevation: 0,
- highlightElevation: 0,
- minWidth: 100,
- height: 36,
- color: primaryColor,
- child: Text(
- '立即更新',
- style: TextStyle(color: Colors.white),
- ),
- onPressed: () async {
- PermissionStatus permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
- if (permission == PermissionStatus.denied) {
- Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.storage]);
- if (permissions[PermissionGroup.storage] == PermissionStatus.denied) {
- showDialog(
- context: context,
- barrierDismissible: false,
- builder: (context) {
- return AlertDialog(
- content: Text(
- '需要获取外部存储权限来下载安装包',
- style: TextStyle(color: Colors.black),
- ),
- actions: <Widget>[
- FlatButton(
- child: Text('打开设置'),
- onPressed: () {
- Navigator.of(context).pop();
- PermissionHandler().openAppSettings();
- },
- ),
- ],
- );
- });
- return;
- }
- }
- Navigator.of(context).pop();
- await downloadApk(context, widget.upgradeInfo.apkUrl);
- },
- ),
- )
- ],
- ),
- )
- ],
- ),
- );
- }
- }
- class DownloadDialog extends StatefulWidget {
- final String url;
- DownloadDialog(this.url, {Key key}) : super(key: key);
- @override
- State<StatefulWidget> createState() {
- return DownloadDialogState();
- }
- }
- class DownloadDialogState extends State<DownloadDialog> {
- final Color primaryColor = Color(0xFF1990F8);
- final Color bgColor = Color(0xE6293559);
- Dio dio;
- CancelToken token;
- double progress = 0;
- @override
- void initState() {
- super.initState();
- Future.delayed(Duration.zero, () async {
- final Directory tempDir = await getExternalStorageDirectory();
- final String tempPath = tempDir.path;
- final String savePath = '$tempPath/update.apk';
- dio = Dio();
- token = CancelToken();
- final res = await dio.download(
- widget.url,
- savePath,
- onReceiveProgress: (int count, int total) {
- setState(() {
- progress = count / total;
- });
- },
- options: Options(receiveTimeout: 10 * 60 * 1000),
- cancelToken: token,
- ).catchError((_error) {
- return _error;
- });
- if (res != null && res.statusCode == 200) {
- OpenFile.open(savePath);
- }
- Navigator.of(context).pop();
- });
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- width: 300,
- height: 60,
- padding: EdgeInsets.fromLTRB(15, 10, 15, 10),
- decoration: BoxDecoration(color: bgColor, border: Border.all(width: 1, color: primaryColor)),
- child: Column(
- children: <Widget>[
- Row(
- children: <Widget>[
- Expanded(
- child: Text(
- '正在下载更新',
- style: TextStyle(color: primaryColor, fontSize: 14),
- ),
- ),
- Text(
- (progress * 100).toStringAsFixed(0) + '%',
- style: TextStyle(color: Colors.white.withAlpha(128)),
- )
- ],
- ),
- Container(
- margin: EdgeInsets.only(top: 10),
- child: Align(
- alignment: Alignment.centerLeft,
- child: FractionallySizedBox(
- widthFactor: progress,
- child: Container(
- height: 2,
- color: primaryColor,
- ),
- ),
- ),
- )
- ],
- ),
- );
- }
- }
|