import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flustars/flustars.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:liftmanager/res/resources.dart'; import 'package:liftmanager/routers/fluro_navigator.dart'; import 'package:liftmanager/utils/theme_utils.dart'; import 'package:liftmanager/utils/toast.dart'; import 'package:liftmanager/utils/version_utils.dart'; class UpdateDialog extends StatefulWidget { UpdateDialog({this.content,this.version,this.url,this.code,this.forceUpdate}); final String content; final int code; final String version; final String url; final bool forceUpdate; @override _UpdateDialogState createState() => _UpdateDialogState(); } class _UpdateDialogState extends State { CancelToken _cancelToken = CancelToken(); bool _isDownload = false; double _value = 0; @override void dispose() { if (!_cancelToken.isCancelled && _value != 1){ _cancelToken.cancel(); } super.dispose(); } @override Widget build(BuildContext context) { Color primaryColor = Theme.of(context).primaryColor; return WillPopScope( onWillPop: () async{ /// 使用false禁止返回键返回,达到强制升级目的 return !widget.forceUpdate; }, child: Scaffold( resizeToAvoidBottomInset: false, backgroundColor: Colors.transparent, body: Center( child: Container( decoration: BoxDecoration( color: ThemeUtils.getDialogBackgroundColor(context), borderRadius: BorderRadius.circular(8.0), ), width: 280.0, child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ Padding( padding: EdgeInsets.only(left: 15.0, right: 15.0, top: 16.0), child: Text("新版本更新 ${widget.version}", style: TextStyles.textSize16), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0), child: Text("${widget.content}"), ), Padding( padding: const EdgeInsets.only(bottom: 15.0, left: 15.0, right: 15.0 , top: 5.0), child: _isDownload ? LinearProgressIndicator( backgroundColor: Colours.line, valueColor: new AlwaysStoppedAnimation(primaryColor), value: _value, ): Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Offstage( offstage:widget.forceUpdate, child: Container( width: 110.0, height: 36.0, child: FlatButton( onPressed: (){ SpUtil.putInt("version_code",widget.code); NavigatorUtils.goBack(context); }, textColor: primaryColor, color: Colors.transparent, disabledTextColor: Colors.white, disabledColor: Colours.text_gray_c, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(18.0), side: BorderSide( color: primaryColor, width: 0.8, ) ), child: Text( "残忍拒绝", style: TextStyle(fontSize: Dimens.font_sp16), ), ), ) ), Container( width: 110.0, height: 36.0, child: FlatButton( onPressed: (){ if (defaultTargetPlatform == TargetPlatform.iOS){ NavigatorUtils.goBack(context); VersionUtils.jumpAppStore(); }else{ setState(() { _isDownload = true; }); _download(); } }, textColor: Colors.white, color: primaryColor, disabledTextColor: Colors.white, disabledColor: Colours.text_gray_c, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(18.0), ), child: Text( "立即更新", style: TextStyle(fontSize: Dimens.font_sp16), ), ), ) ], ), ) ], ) ), ) ), ); } ///下载apk _download() async { try { await DirectoryUtil.getInstance(); DirectoryUtil.createStorageDirSync(category: 'apk'); String path = DirectoryUtil.getStoragePath(fileName: 'liftmanager-${widget.version}', category: 'apk', format: 'apk'); File file = File(path); /// 链接可能会失效 await Dio().download("${widget.url}", file.path, cancelToken: _cancelToken, onReceiveProgress: (int count, int total){ if (total != -1) { _value = count / total; setState(() { }); if (count == total){ NavigatorUtils.goBack(context); print("path:::"); print(path); VersionUtils.install(path); } } }, ); }catch (e){ toasts("下载失败"); print(e); setState(() { _isDownload = false; }); } } }