update_dialog.dart 6.2 KB
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:Parlando/res/resources.dart';
import 'package:Parlando/util/other_utils.dart';
import 'package:Parlando/routers/fluro_navigator.dart';
import 'package:Parlando/util/image_utils.dart';
import 'package:Parlando/util/theme_utils.dart';
import 'package:Parlando/util/toast_utils.dart';
import 'package:Parlando/util/version_utils.dart';
import 'package:Parlando/widgets/my_button.dart';

import 'package:Parlando/extension/int_extension.dart';

class UpdateDialog extends StatefulWidget {
  const UpdateDialog({Key? key}) : super(key: key);

  @override
  _UpdateDialogState createState() => _UpdateDialogState();
}

class _UpdateDialogState extends State<UpdateDialog> {
  final 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) {
    final Color primaryColor = Theme.of(context).primaryColor;
    return WillPopScope(
      onWillPop: () async {
        /// 使用false禁止返回键返回,达到强制升级目的
        return true;
      },
      child: Scaffold(
          resizeToAvoidBottomInset: false,
          backgroundColor: Colors.transparent,
          body: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Container(
                  height: 120.px,
                  width: 280.px,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(8.px),
                        topRight: Radius.circular(8.px)),
                    image: DecorationImage(
                      image: ImageUtils.getAssetImage('update_head',
                          format: ImageFormat.jpg),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                Container(
                  width: 280.px,
                  decoration: BoxDecoration(
                    color: context.dialogBackgroundColor,
                    borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(8.px),
                      bottomRight: Radius.circular(8.px),
                    ),
                  ),
                  padding: EdgeInsets.symmetric(
                    horizontal: 15.px,
                    vertical: 15.px,
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      const Text('新版本更新', style: TextStyles.textSize16),
                      Gaps.vGap10,
                      const Text('1.又双叒修复了一大堆bug。\n\n2.祭天了多名程序猿。'),
                      Gaps.vGap15,
                      if (_isDownload)
                        LinearProgressIndicator(
                          backgroundColor: Colours.line,
                          valueColor:
                              AlwaysStoppedAnimation<Color>(primaryColor),
                          value: _value,
                        )
                      else
                        _buildButton(context),
                    ],
                  ),
                ),
              ],
            ),
          )),
    );
  }

  Widget _buildButton(BuildContext context) {
    final Color primaryColor = Theme.of(context).primaryColor;
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        SizedBox(
          width: 110.px,
          height: 36.px,
          child: MyButton(
            text: '残忍拒绝',
            fontSize: Dimens.font_sp16,
            textColor: primaryColor,
            disabledTextColor: Colors.white,
            disabledBackgroundColor: Colours.text_gray_c,
            radius: 18.px,
            side: BorderSide(
              color: primaryColor,
              width: 0.8,
            ),
            backgroundColor: Colors.transparent,
            onPressed: () {
              NavigatorUtils.goBack(context);
            },
          ),
        ),
        SizedBox(
          width: 110.px,
          height: 36.px,
          child: MyButton(
            text: '立即更新',
            fontSize: Dimens.font_sp16,
            onPressed: () {
              if (defaultTargetPlatform == TargetPlatform.iOS) {
                NavigatorUtils.goBack(context);
                VersionUtils.jumpAppStore();
              } else {
                setState(() {
                  _isDownload = true;
                });
                _download();
              }
            },
            textColor: Colors.white,
            backgroundColor: primaryColor,
            disabledTextColor: Colors.white,
            disabledBackgroundColor: Colours.text_gray_c,
            radius: 18.px,
          ),
        )
      ],
    );
  }

  ///下载apk
  Future<void> _download() async {
    try {
      setInitDir(initStorageDir: true);
      await DirectoryUtil.getInstance();
      DirectoryUtil.createStorageDirSync(category: 'Download');
      final String path = DirectoryUtil.getStoragePath(
              fileName: 'deer', category: 'Download', format: 'apk')
          .nullSafe;
      final File file = File(path);

      /// TODO 需要替换成正式APP地址
      await Dio().download(
        'http://imtt.dd.qq.com/16891/apk/FF9625F40FD26F015F4CDED37B6B66AE.apk',
        file.path,
        cancelToken: _cancelToken,
        onReceiveProgress: (int count, int total) {
          if (total != -1) {
            _value = count / total;
            setState(() {});
            if (count == total) {
              NavigatorUtils.goBack(context);
              VersionUtils.install(path);
            }
          }
        },
      );
    } catch (e) {
      Toast.show('下载失败!');
      debugPrint(e.toString());
      setState(() {
        _isDownload = false;
      });
    }
  }
}