update_dialog.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import 'dart:io';
  2. import 'package:dio/dio.dart';
  3. import 'package:flustars/flustars.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:liftmanager/res/resources.dart';
  7. import 'package:liftmanager/routers/fluro_navigator.dart';
  8. import 'package:liftmanager/utils/theme_utils.dart';
  9. import 'package:liftmanager/utils/toast.dart';
  10. import 'package:liftmanager/utils/version_utils.dart';
  11. class UpdateDialog extends StatefulWidget {
  12. UpdateDialog({this.content,this.version,this.url,this.code,this.forceUpdate});
  13. final String content;
  14. final int code;
  15. final String version;
  16. final String url;
  17. final bool forceUpdate;
  18. @override
  19. _UpdateDialogState createState() => _UpdateDialogState();
  20. }
  21. class _UpdateDialogState extends State<UpdateDialog> {
  22. CancelToken _cancelToken = CancelToken();
  23. bool _isDownload = false;
  24. double _value = 0;
  25. @override
  26. void dispose() {
  27. if (!_cancelToken.isCancelled && _value != 1){
  28. _cancelToken.cancel();
  29. }
  30. super.dispose();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. Color primaryColor = Theme.of(context).primaryColor;
  35. return WillPopScope(
  36. onWillPop: () async{
  37. /// 使用false禁止返回键返回,达到强制升级目的
  38. return !widget.forceUpdate;
  39. },
  40. child: Scaffold(
  41. resizeToAvoidBottomInset: false,
  42. backgroundColor: Colors.transparent,
  43. body: Center(
  44. child: Container(
  45. decoration: BoxDecoration(
  46. color: ThemeUtils.getDialogBackgroundColor(context),
  47. borderRadius: BorderRadius.circular(8.0),
  48. ),
  49. width: 280.0,
  50. child: Column(
  51. crossAxisAlignment: CrossAxisAlignment.start,
  52. mainAxisSize: MainAxisSize.min,
  53. children: <Widget>[
  54. Padding(
  55. padding: EdgeInsets.only(left: 15.0, right: 15.0, top: 16.0),
  56. child: Text("新版本更新 ${widget.version}", style: TextStyles.textSize16),
  57. ),
  58. Padding(
  59. padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
  60. child: Text("${widget.content}"),
  61. ),
  62. Padding(
  63. padding: const EdgeInsets.only(bottom: 15.0, left: 15.0, right: 15.0 , top: 5.0),
  64. child: _isDownload ? LinearProgressIndicator(
  65. backgroundColor: Colours.line,
  66. valueColor: new AlwaysStoppedAnimation<Color>(primaryColor),
  67. value: _value,
  68. ): Row(
  69. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  70. children: <Widget>[
  71. Offstage(
  72. offstage:widget.forceUpdate,
  73. child: Container(
  74. width: 110.0,
  75. height: 36.0,
  76. child: FlatButton(
  77. onPressed: (){
  78. SpUtil.putInt("version_code",widget.code);
  79. NavigatorUtils.goBack(context);
  80. },
  81. textColor: primaryColor,
  82. color: Colors.transparent,
  83. disabledTextColor: Colors.white,
  84. disabledColor: Colours.text_gray_c,
  85. shape: RoundedRectangleBorder(
  86. borderRadius: BorderRadius.circular(18.0),
  87. side: BorderSide(
  88. color: primaryColor,
  89. width: 0.8,
  90. )
  91. ),
  92. child: Text(
  93. "残忍拒绝",
  94. style: TextStyle(fontSize: Dimens.font_sp16),
  95. ),
  96. ),
  97. )
  98. ),
  99. Container(
  100. width: 110.0,
  101. height: 36.0,
  102. child: FlatButton(
  103. onPressed: (){
  104. if (defaultTargetPlatform == TargetPlatform.iOS){
  105. NavigatorUtils.goBack(context);
  106. VersionUtils.jumpAppStore();
  107. }else{
  108. setState(() {
  109. _isDownload = true;
  110. });
  111. _download();
  112. }
  113. },
  114. textColor: Colors.white,
  115. color: primaryColor,
  116. disabledTextColor: Colors.white,
  117. disabledColor: Colours.text_gray_c,
  118. shape: RoundedRectangleBorder(
  119. borderRadius: BorderRadius.circular(18.0),
  120. ),
  121. child: Text(
  122. "立即更新",
  123. style: TextStyle(fontSize: Dimens.font_sp16),
  124. ),
  125. ),
  126. )
  127. ],
  128. ),
  129. )
  130. ],
  131. )
  132. ),
  133. )
  134. ),
  135. );
  136. }
  137. ///下载apk
  138. _download() async {
  139. try {
  140. await DirectoryUtil.getInstance();
  141. DirectoryUtil.createStorageDirSync(category: 'apk');
  142. String path = DirectoryUtil.getStoragePath(fileName: 'liftmanager-${widget.version}', category: 'apk', format: 'apk');
  143. File file = File(path);
  144. /// 链接可能会失效
  145. await Dio().download("${widget.url}",
  146. file.path,
  147. cancelToken: _cancelToken,
  148. onReceiveProgress: (int count, int total){
  149. if (total != -1) {
  150. _value = count / total;
  151. setState(() {
  152. });
  153. if (count == total){
  154. NavigatorUtils.goBack(context);
  155. print("path:::");
  156. print(path);
  157. VersionUtils.install(path);
  158. }
  159. }
  160. },
  161. );
  162. }catch (e){
  163. toasts("下载失败");
  164. print(e);
  165. setState(() {
  166. _isDownload = false;
  167. });
  168. }
  169. }
  170. }