2019独角兽企业重金招聘Python工程师标准>>>
前言:
Flutter的APP打开后检测到有新版本后,可以直接到应用市场去下载新的APK,也可以使用flutter_downloader从服务器下载。因为我们的应用是上架到Google Play,当用户点击下载的按钮,直接跳转到Google Play官网去下载APK即可。下面我简单总结一下在Flutter中实现“版本更新”的功能。
思路:
1.第一次打开APP时执行"版本更新"的网络请求;
2.比较服务器的版本号跟当前的版本号,来判断要不要升级APP应用程序;
3.弹出“版本更新”对话框。
实现的步骤:
1.配置AndroidMenifest.xml文件
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 {
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 _showNewVersionAppDialog() async {
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: new Row(
children: [
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: [
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 {
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(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: new Row(
children: [
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: [
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{
@override
Widget build(BuildContext context) {
return UpdaterPage(Scaffold( //调用UpdaterPage
appBar: _buildAppBar(),
body: getScreen()
));
}
}
10.总结
当有新版本需要升级时,客户端会"版本更新"弹出对话框,用户点击下载的按钮直接跳转到Google Play官网去下载APK就可以。APP版本更新的功能已经实现,欢迎大家围观。如果有什么疑问的话,可以留言联系我哦!