Browse Source

app自动更新

remove_uniapp
w-R 3 years ago
parent
commit
e081231709
  1. 36
      lib/main_page.dart
  2. 15
      lib/retrofit/data/app_update.dart
  3. 2
      lib/setting/about_page.dart
  4. 13
      lib/utils/flutter_utils.dart
  5. 17
      lib/view_widget/update_dialog.dart

36
lib/main_page.dart

@ -20,9 +20,11 @@ import 'package:huixiang/union/union_page.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/utils/bridge.dart'; import 'package:huixiang/utils/bridge.dart';
import 'package:huixiang/utils/event_type.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/font_weight.dart';
import 'package:huixiang/utils/native_event_handler.dart'; import 'package:huixiang/utils/native_event_handler.dart';
import 'package:huixiang/view_widget/update_dialog.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:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:sharesdk_plugin/sharesdk_interface.dart'; import 'package:sharesdk_plugin/sharesdk_interface.dart';
@ -50,8 +52,6 @@ class _MainPage extends State<MainPage> with WidgetsBindingObserver {
final GlobalKey homePageKey = GlobalKey(); final GlobalKey homePageKey = GlobalKey();
// List<String> iconn; // List<String> iconn;
ApiService apiService; ApiService apiService;
String version = "1.0.1";
AppUpdate appUpdate;
@override @override
@ -80,7 +80,6 @@ class _MainPage extends State<MainPage> with WidgetsBindingObserver {
void initState() { void initState() {
super.initState(); super.initState();
WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addObserver(this);
EasyLoading.instance EasyLoading.instance
..indicatorType = EasyLoadingIndicatorType.circle ..indicatorType = EasyLoadingIndicatorType.circle
// ..indicatorType = EasyLoadingIndicatorType.wave // ..indicatorType = EasyLoadingIndicatorType.wave
@ -327,19 +326,30 @@ class _MainPage extends State<MainPage> with WidgetsBindingObserver {
///app自动更新 ///app自动更新
appAutoUpdate() async { appAutoUpdate() async {
SharedPreferences value = await SharedPreferences.getInstance();
if (apiService == null) { if (apiService == null) {
SharedPreferences value = await SharedPreferences.getInstance();
apiService = ApiService( apiService = ApiService(
Dio(), Dio(),
context: context, context: context,
token: value.getString("token"), token: value.getString("token"),
showLoading: true
); );
} }
PackageInfo packageInfo = await PackageInfo.fromPlatform();
BaseData<AppUpdate> baseData = await apiService.appVersion().catchError((onError) {}); BaseData<AppUpdate> baseData = await apiService.appVersion().catchError((onError) {});
if (baseData != null && baseData.isSuccess) { if (baseData != null && baseData.isSuccess) {
if(version != appUpdate?.appLastVersion ?? ""){ if(AppUtils.versionCompare(packageInfo.version,baseData.data?.appLastVersion)
showAlertDialog(version); && 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{ }else{
return; return;
} }
@ -475,18 +485,6 @@ class _MainPage extends State<MainPage> with WidgetsBindingObserver {
); );
} }
///app更新弹窗
showAlertDialog(version) {
//
showDialog(
context: context,
builder: (BuildContext context) {
return UpdateDialog(version,appUpdate);
},
);
}
// Widget bottomNavigationBigItem(text, index) { // Widget bottomNavigationBigItem(text, index) {
// var isSelected = index == clickIndex; // var isSelected = index == clickIndex;
// return Expanded( // return Expanded(

15
lib/retrofit/data/app_update.dart

@ -31,6 +31,10 @@ class AppUpdate {
String get appLastVersionUp => _appLastVersionUp; String get appLastVersionUp => _appLastVersionUp;
String get appLastVersionExplain => _appLastVersionExplain; String get appLastVersionExplain => _appLastVersionExplain;
set appLastVersion(String value) {
_appLastVersion = value;
}
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final map = <String, dynamic>{}; final map = <String, dynamic>{};
map['appLastVersion'] = _appLastVersion; map['appLastVersion'] = _appLastVersion;
@ -40,4 +44,15 @@ class AppUpdate {
return map; return map;
} }
set appLastVersionUrl(String value) {
_appLastVersionUrl = value;
}
set appLastVersionUp(String value) {
_appLastVersionUp = value;
}
set appLastVersionExplain(String value) {
_appLastVersionExplain = value;
}
} }

2
lib/setting/about_page.dart

@ -19,7 +19,7 @@ class AboutPage extends StatefulWidget {
} }
class _AboutPage extends State<AboutPage> { class _AboutPage extends State<AboutPage> {
String version = "1.0.1"; String version = "";
@override @override
void initState() { void initState() {

13
lib/utils/flutter_utils.dart

@ -16,6 +16,19 @@ class AppUtils {
return "${numberFormat.format(mi / 1000)}"; 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){ static String trimEnd(String resString,String char){
while(resString.endsWith(char)){ while(resString.endsWith(char)){
resString = resString.substring(0,resString.length - 1); resString = resString.substring(0,resString.length - 1);

17
lib/view_widget/update_dialog.dart

@ -4,8 +4,10 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:huixiang/retrofit/data/app_update.dart'; import 'package:huixiang/retrofit/data/app_update.dart';
import 'package:huixiang/utils/bridge.dart'; import 'package:huixiang/utils/bridge.dart';
import 'package:huixiang/utils/flutter_utils.dart';
import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/utils/font_weight.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
class UpdateDialog extends StatefulWidget { class UpdateDialog extends StatefulWidget {
@ -43,7 +45,7 @@ class _UpdateDialog extends State<UpdateDialog> {
child: Column( child: Column(
children: [ children: [
Text( Text(
"检测到新版本 v${widget.version}", "检测到新版本 v${widget.appUpdate.appLastVersion}",
style: TextStyle( style: TextStyle(
fontSize: 17.sp, fontSize: 17.sp,
fontWeight: MyFontWeight.semi_bold, fontWeight: MyFontWeight.semi_bold,
@ -71,10 +73,11 @@ class _UpdateDialog extends State<UpdateDialog> {
child: Row( child: Row(
mainAxisSize: MainAxisSize.max, mainAxisSize: MainAxisSize.max,
children: [ children: [
if(!AppUtils.versionCompare(widget.version,widget.appUpdate.appLastVersionUp))
Expanded( Expanded(
child: InkWell( child: InkWell(
onTap: () { onTap:() {
Navigator.of(context).pop(); doNotUpdate();
}, },
child: Container( child: Container(
width: double.infinity, width: double.infinity,
@ -102,7 +105,7 @@ class _UpdateDialog extends State<UpdateDialog> {
(Theme.of(context).platform == TargetPlatform.android) ? (Theme.of(context).platform == TargetPlatform.android) ?
androidUpdate():Bridge.toAppStore().then((value) { androidUpdate():Bridge.toAppStore().then((value) {
}); });
Navigator.of(context).pop(); // Navigator.of(context).pop();
}, },
child: Container( child: Container(
width: double.infinity, width: double.infinity,
@ -130,6 +133,12 @@ class _UpdateDialog extends State<UpdateDialog> {
); );
} }
doNotUpdate() async {
SharedPreferences value = await SharedPreferences.getInstance();
value.setString("appLastVersion", widget.appUpdate.appLastVersion);
Navigator.of(context).pop();
}
androidUpdate() async { androidUpdate() async {
String url = "http://application.lotus-wallet.com/huixaing?release_id=627cadff23389f2fe6d57cb1"; String url = "http://application.lotus-wallet.com/huixaing?release_id=627cadff23389f2fe6d57cb1";
if (await canLaunch(url)) { if (await canLaunch(url)) {

Loading…
Cancel
Save