flutter简单的app升级方案

  • flutter简单的app升级方案已关闭评论
  • 4 views
  • A+
所属分类:flutter

升级过程中包括强制升级和普通升级

普通升级,用户自己选择升级

强制升级,用户必须升级,否则退出app

效果如下图:

flutter简单的app升级方案

代码如下:

import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:zmreader/common/index.dart';
import 'package:zmreader/model/app_update_model.dart';
import 'package:zmreader/ui/widget/dialog.dart';
import 'package:zmreader/utils/http_utils.dart';



class UpdateDialog extends StatefulWidget {

  final String savePath;

  final AppUpdate appUpdate;

  UpdateDialog({Key key, @required this.appUpdate, @required this.savePath})
      : super(key: key);

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

class _UpdateDialogState extends State<UpdateDialog> {
  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) {
    return WillPopScope(
      onWillPop: () async {
        /// 使用false禁止返回键返回,达到强制升级目的
        return this.widget.appUpdate.iscompel=="1"?false:true;
      },
      child: Scaffold(
          resizeToAvoidBottomInset: false,
          backgroundColor: Colors.transparent,
          body: Center(
            child: Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(8.0)),
                width: 280.0,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Container(
                        height: 140.0,
                        width: 280.0,
                        decoration: BoxDecoration(
                          borderRadius: const BorderRadius.only(
                              topLeft: const Radius.circular(8.0),
                              topRight: const Radius.circular(8.0)),
                          image: DecorationImage(
                              image: AssetImage(ImageUtils.loactionImg("update_head")),
                              fit: BoxFit.cover),
                        )),
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: 15.0),
                      child: Text("新版本更新", style: TextStyle(fontSize: 16,color: Colours.gray_66)),
                    ),
                    Padding(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 15.0, vertical: 10.0),
                        child: Text(this.widget.appUpdate.description,
 style: TextStyle(fontSize: 14,color: Colours.darkGray))),
 Padding(
 padding: const EdgeInsets.only(
 bottom: 15.0, left: 15.0, right: 15.0, top: 5.0),
 child: _isDownload
 ? LinearProgressIndicator(
 backgroundColor: Colors.grey,
 valueColor: AlwaysStoppedAnimation<Color>(
 Colors.lightBlue),
 value: _value)
 : Row(
 mainAxisAlignment: MainAxisAlignment.spaceBetween,
 children: <Widget>[
 Container(
 width: 110.0,
 height: 36.0,
 child: FlatButton(
 onPressed: () {
 if(this.widget.appUpdate.iscompel=="1"){
 SystemChannels.platform.invokeMethod('SystemNavigator.pop');
 }else{
 Navigator.of(context).pop();
 }
 },
 textColor: Colors.lightBlue,
 color: Colors.transparent,
 disabledTextColor: Colors.white,
 disabledColor: Colors.grey,
 shape: RoundedRectangleBorder(
 borderRadius:
 BorderRadius.circular(18.0),
 side: BorderSide(
 color: Colors.lightBlue,
 width: 0.8)),
 child: Text("残忍拒绝",
 style: TextStyle(fontSize: 16,color: Colours.blue)),
 ),
 ),
 Container(
 width: 110.0,
 height: 36.0,
 child: FlatButton(
 onPressed: () {
 if (defaultTargetPlatform ==
 TargetPlatform.iOS) {
 Navigator.of(context).pop();

 /// TODO 跳转到APP Store
 } else {
 setState(() {
 _isDownload = true;
 });
 _download(context, widget.appUpdate.appurl,
 widget.savePath);
 }
 },
 textColor: Colors.white,
 color: Colors.lightBlue,
 disabledTextColor: Colors.white,
 disabledColor: Colors.grey,
 shape: RoundedRectangleBorder(
 borderRadius: BorderRadius.circular(18.0),
 ),
 child: Text("立即更新",
 style: TextStyle(fontSize:16 )),
 ),
 )
 ],
 ),
 )
 ],
 )),
 )),
 );
 }

 /// 下载apk
 _download(BuildContext context, String url, String savePath) async {

 Directory basedirectory = await getApplicationDocumentsDirectory();
 try {
 HttpUtils.downloadfile(url, "${basedirectory.path}/${savePath}",
 onReceiveProgress: (int count, int total) async {
 setState(() => _value = count / total);

 if (count == total) {
 await OpenFile.open("${basedirectory.path}/${savePath}");
 await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
 }
 }, cancelToken: _cancelToken);
 } catch (e) {
 Fluttertoast.showToast(msg: "下载失败!",gravity: ToastGravity.CENTER,fontSize: 14.0);
 }
 }

}

 

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar