Flutter开发 APP版本更新

前言:

      Flutter的APP打开后检测到有新版本后,可以直接到应用市场去下载新的APK,也可以使用flutter_downloader从服务器下载。因为我们的应用是上架到Google Play,当用户点击下载的按钮,直接跳转到Google Play官网去下载APK即可。下面我简单总结一下在Flutter中实现“版本更新”的功能。

思路:

1.第一次打开APP时执行"版本更新"的网络请求;

2.比较服务器的版本号跟当前的版本号,来判断要不要升级APP应用程序;

3.弹出“版本更新”对话框。

实现的步骤:

1.配置AndroidMenifest.xml文件

 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

2.在pubspec.yaml添加sdk

dependencies:
  ...
  cupertino_icons: ^0.1.0
  package_info: ^0.3.2+1

3.导包

import 'package:package_info/package_info.dart';

4.第一次打开APP时执行"版本更新"的网络请求

class UpdatePagerState extends State<UpdaterPage> {
  var _serviceVersionCode;

  @override
  void initState() {
    super.initState();
    _getNewVersionAPP();
  }

  //执行版本更新的网络请求
  _getNewVersionAPP() async {
    String url = IHttpService.getNewVersionApp;
    DioUtil.getInstance().get(context, url).then((response) {
      if (response != null) {
        setState(() {
          var data = response.data;
          _serviceVersionCode = data["versionCode"];
          _checkVersionCode();
        });
      }
    });
  }

}

5.比较服务器的版本号跟当前的版本号,来判断要不要升级APP应用程序

void _checkVersionCode() {
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      var currentVersionCode = packageInfo.version;//获取当前的版本号
      //如果获取服务器的版本号比当前应用程序的版本号还高,那么提示升级
      if (serviceVersionCode > currentVersionCode) {
        _showNewVersionAppDialog();//弹出"版本更新"的对话框
      }
   });
}

6.弹出“版本更新”对话框

 Future<void> _showNewVersionAppDialog() async {
    return showDialog<void>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: new Row(
              children: <Widget>[
                new Image.asset("images/ic_launcher_icon.png",
                    height: 35.0, width: 35.0),
                new Padding(
                    padding: const EdgeInsets.fromLTRB(30.0, 0.0, 10.0, 0.0),
                    child: new Text("项目名称",
                        style: dialogButtonTextStyle))
              ],
            ),
            content: new Text(
                '版本更新',
                style: dialogTextStyle),
            actions: <Widget>[
              new FlatButton(
                child: new Text('Later', style: dialogButtonTextStyle),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
              new FlatButton(
                child: new Text('DownLoad', style: dialogButtonTextStyle),
                onPressed: () {
//                  launch("https://play.google.com/store/apps/details?id=项目包名");
                  _launchGooglePlay();//到Google Play官网去下载APK
                  Navigator.of(context).pop();              
                },
              )
            ],
          );
        });
  }

7.到Google Play官网去下载APK

   _launchGooglePlay() async{
       String url = "https://play.google.com/store/apps/details?id=项目包名";
       if (await canLaunch(url)) {
         await launch(url);
       } else {
         throw 'Could not launch $url';
    }
  }

8.完整的代码

class UpdaterPage extends StatefulWidget {
  final Widget child;

  const UpdaterPage(this.child);

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

class UpdatePagerState extends State<UpdaterPage> {
  var _serviceVersionCode;

  @override
  void initState() {
    super.initState();
    _getNewVersionAPP();
  }

  //执行版本更新的网络请求
  _getNewVersionAPP() async {
    String url = IHttpService.getNewVersionApp;
    DioUtil.getInstance().get(context, url).then((response) {
      if (response != null) {
        setState(() {
          var data = response.data;
          _serviceVersionCode = data["versionCode"];
          _checkVersionCode();
        });
      }
    });
  }

  //检查版本更新的版本号
  _checkVersionCode() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String _currentVersionCode = packageInfo.version;
    int currentVersionCode = int.parse(_currentVersionCode);
    if (_serviceVersionCode > currentVersionCode) {
      _showNewVersionAppDialog();
    }
  }

 //弹出"版本更新"对话框
  _showNewVersionAppDialog() async {
    return showDialog<void>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: new Row(
              children: <Widget>[
                new Image.asset("images/ic_launcher_icon.png",
                    height: 35.0, width: 35.0),
                new Padding(
                    padding: const EdgeInsets.fromLTRB(30.0, 0.0, 10.0, 0.0),
                    child: new Text(Strings.new_version_title,
                        style: dialogButtonTextStyle))
              ],
            ),
            content: new Text(Strings.new_version_dialog_content,
                style: dialogTextStyle),
            actions: <Widget>[
              new FlatButton(
                child: new Text(Strings.new_version_button_later,
                    style: dialogButtonTextStyle),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
              new FlatButton(
                child: new Text(Strings.new_version_button_download,
                    style: dialogButtonTextStyle),
                onPressed: () {
//                launch("https://play.google.com/store/apps/details?id=项目包名");
                  _launchGooglePlay();
                  Navigator.of(context).pop();
                },
              )
            ],
          );
        });
  }

  _launchGooglePlay() async{
       String url = "https://play.google.com/store/apps/details?id=项目包名";
       if (await canLaunch(url)) {
         await launch(url);
       } else {
         throw 'Could not launch $url';
    }
  }

  @override
  Widget build(BuildContext context) => widget.child;
}

9.在首页调用”版本更新”的类

class IndexPage extends StatefulWidget {
  @override
  IndexPageState createState() => IndexPageState();
}
 
class IndexPageState extends State<IndexPage>{
  @override
  Widget build(BuildContext context) {
     return UpdaterPage(Scaffold( //调用UpdaterPage
      appBar: _buildAppBar(),
      body: getScreen()
    ));
  }
}

10.总结

      当有新版本需要升级时,客户端会"版本更新"弹出对话框,用户点击下载的按钮直接跳转到Google Play官网去下载APK就可以。APP版本更新的功能已经实现,欢迎大家围观。如果有什么疑问的话,可以留言联系我哦!

 

转载于:https://my.oschina.net/wupeilin/blog/3035732

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值