diff --git a/lib/main_page.dart b/lib/main_page.dart index 58acba26..2fb43dbb 100644 --- a/lib/main_page.dart +++ b/lib/main_page.dart @@ -20,9 +20,11 @@ import 'package:huixiang/union/union_page.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:huixiang/utils/bridge.dart'; import 'package:huixiang/utils/event_type.dart'; +import 'package:huixiang/utils/flutter_utils.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/utils/native_event_handler.dart'; import 'package:huixiang/view_widget/update_dialog.dart'; +import 'package:package_info/package_info.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sharesdk_plugin/sharesdk_interface.dart'; @@ -50,8 +52,6 @@ class _MainPage extends State with WidgetsBindingObserver { final GlobalKey homePageKey = GlobalKey(); // List iconn; ApiService apiService; - String version = "1.0.1"; - AppUpdate appUpdate; @override @@ -80,7 +80,6 @@ class _MainPage extends State with WidgetsBindingObserver { void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); - EasyLoading.instance ..indicatorType = EasyLoadingIndicatorType.circle // ..indicatorType = EasyLoadingIndicatorType.wave @@ -327,19 +326,30 @@ class _MainPage extends State with WidgetsBindingObserver { ///app自动更新 appAutoUpdate() async { + SharedPreferences value = await SharedPreferences.getInstance(); if (apiService == null) { - SharedPreferences value = await SharedPreferences.getInstance(); apiService = ApiService( Dio(), context: context, token: value.getString("token"), - showLoading: true ); } + PackageInfo packageInfo = await PackageInfo.fromPlatform(); BaseData baseData = await apiService.appVersion().catchError((onError) {}); if (baseData != null && baseData.isSuccess) { - if(version != appUpdate?.appLastVersion ?? ""){ - showAlertDialog(version); + if(AppUtils.versionCompare(packageInfo.version,baseData.data?.appLastVersion) + && AppUtils.versionCompare(value.getString("appLastVersion")??"1.0.0",baseData.data?.appLastVersion)){ + showDialog( + context: context, + barrierDismissible: false, + builder: (BuildContext context) { + return WillPopScope( + child:UpdateDialog(packageInfo.version,baseData.data), + onWillPop: () async { + return Future.value(false); + }); + }, + ); }else{ return; } @@ -475,18 +485,6 @@ class _MainPage extends State with WidgetsBindingObserver { ); } - ///app更新弹窗 - showAlertDialog(version) { - //显示对话框 - showDialog( - context: context, - builder: (BuildContext context) { - return UpdateDialog(version,appUpdate); - }, - ); - } - - // Widget bottomNavigationBigItem(text, index) { // var isSelected = index == clickIndex; // return Expanded( diff --git a/lib/retrofit/data/app_update.dart b/lib/retrofit/data/app_update.dart index cac55b14..2185a187 100644 --- a/lib/retrofit/data/app_update.dart +++ b/lib/retrofit/data/app_update.dart @@ -31,6 +31,10 @@ class AppUpdate { String get appLastVersionUp => _appLastVersionUp; String get appLastVersionExplain => _appLastVersionExplain; + set appLastVersion(String value) { + _appLastVersion = value; + } + Map toJson() { final map = {}; map['appLastVersion'] = _appLastVersion; @@ -40,4 +44,15 @@ class AppUpdate { return map; } + set appLastVersionUrl(String value) { + _appLastVersionUrl = value; + } + + set appLastVersionUp(String value) { + _appLastVersionUp = value; + } + + set appLastVersionExplain(String value) { + _appLastVersionExplain = value; + } } \ No newline at end of file diff --git a/lib/setting/about_page.dart b/lib/setting/about_page.dart index 80d31310..20514dcc 100644 --- a/lib/setting/about_page.dart +++ b/lib/setting/about_page.dart @@ -19,7 +19,7 @@ class AboutPage extends StatefulWidget { } class _AboutPage extends State { - String version = "1.0.1"; + String version = ""; @override void initState() { diff --git a/lib/utils/flutter_utils.dart b/lib/utils/flutter_utils.dart index 47711755..12273ff4 100644 --- a/lib/utils/flutter_utils.dart +++ b/lib/utils/flutter_utils.dart @@ -16,6 +16,19 @@ class AppUtils { return "${numberFormat.format(mi / 1000)}"; } + ///版本号 + static bool versionCompare(String localVersion,String serverVersion){ + try{ + var localVersionArr = localVersion.split("."); + var serverVersionArr = serverVersion.split("."); + if((int.tryParse(localVersionArr[0]) < int.tryParse(serverVersionArr[0])) + || (int.tryParse(localVersionArr[1]) < int.tryParse(serverVersionArr[1])) + || (int.tryParse(localVersionArr[2]) < int.tryParse(serverVersionArr[2]))) + return true; + }catch(ex){ } + return false; + } + static String trimEnd(String resString,String char){ while(resString.endsWith(char)){ resString = resString.substring(0,resString.length - 1); diff --git a/lib/view_widget/update_dialog.dart b/lib/view_widget/update_dialog.dart index 2d590c1d..fa42f586 100644 --- a/lib/view_widget/update_dialog.dart +++ b/lib/view_widget/update_dialog.dart @@ -4,8 +4,10 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter/material.dart'; import 'package:huixiang/retrofit/data/app_update.dart'; import 'package:huixiang/utils/bridge.dart'; +import 'package:huixiang/utils/flutter_utils.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:path_provider/path_provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:url_launcher/url_launcher.dart'; class UpdateDialog extends StatefulWidget { @@ -43,7 +45,7 @@ class _UpdateDialog extends State { child: Column( children: [ Text( - "检测到新版本 v${widget.version}", + "检测到新版本 v${widget.appUpdate.appLastVersion}", style: TextStyle( fontSize: 17.sp, fontWeight: MyFontWeight.semi_bold, @@ -71,10 +73,11 @@ class _UpdateDialog extends State { child: Row( mainAxisSize: MainAxisSize.max, children: [ + if(!AppUtils.versionCompare(widget.version,widget.appUpdate.appLastVersionUp)) Expanded( child: InkWell( - onTap: () { - Navigator.of(context).pop(); + onTap:() { + doNotUpdate(); }, child: Container( width: double.infinity, @@ -102,7 +105,7 @@ class _UpdateDialog extends State { (Theme.of(context).platform == TargetPlatform.android) ? androidUpdate():Bridge.toAppStore().then((value) { }); - Navigator.of(context).pop(); + // Navigator.of(context).pop(); }, child: Container( width: double.infinity, @@ -130,6 +133,12 @@ class _UpdateDialog extends State { ); } + doNotUpdate() async { + SharedPreferences value = await SharedPreferences.getInstance(); + value.setString("appLastVersion", widget.appUpdate.appLastVersion); + Navigator.of(context).pop(); + } + androidUpdate() async { String url = "http://application.lotus-wallet.com/huixaing?release_id=627cadff23389f2fe6d57cb1"; if (await canLaunch(url)) {