import 'dart:io'; import 'package:device_info/device_info.dart'; import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; import 'package:package_info/package_info.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sport/bean/UpdateInfo.dart'; import 'package:sport/services/api/resp.dart'; import 'package:sport/services/api/rest_client.dart'; import 'package:sport/utils/toast.dart'; import 'package:sport/utils/version.dart'; import 'package:sport/widgets/dialog/alert_dialog.dart'; import 'package:url_launcher/url_launcher.dart'; updateApp(BuildContext context) async { updateApk(context); } Future> checkUpdate() async { String version = (await PackageInfo.fromPlatform()).version; final api = GetIt.I(); RespData data = await api.checkUpdate(version).catchError((err) {}); return data; } updateApk(BuildContext context, {bool tips = false}) async { SharedPreferences prefs = await SharedPreferences.getInstance(); String? token = prefs.getString("token"); if (token?.isEmpty == true) return; RespData data = await checkUpdate(); if (data.code == 0) { UpdateInfo? info = data.data; if (info?.needUpdate == false) { if (tips == true) { ToastUtil.show("当前已是最新版本"); } return; } if (info?.lastVersion == null) return; try { DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); if (Platform.isAndroid) { AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo; AndroidBuildVersion buildVersion = androidDeviceInfo.version; if (buildVersion.sdkInt < int.parse(info?.lastVersion?.minVer ?? "0")) { return; } } else if (Platform.isIOS) { IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo; if (versionCompare(iosDeviceInfo.systemVersion, info?.lastVersion?.minVer ?? "0") < 0) { return; } } } catch (e) { print(e); } var result = await showDialog( context: context, builder: (context) => CustomAlertDialog( title: '发现新版本 ${info?.lastVersion?.version}', child: Container( width: double.infinity, padding: const EdgeInsets.symmetric(horizontal: 24.0), child: Text( "${info?.lastVersion?.detail?.replaceAll("\\n", "\n")}", style: TextStyle(fontSize: 14, color: Color(0xff333333), height: 1.4), )), textOk: '立即更新', ok: () => Navigator.of(context).pop(true)), ); if (result == true) { if (Platform.isAndroid) { launch(info?.lastVersion?.downloadUrl ?? ""); } else if (Platform.isIOS) { launch("https://apps.apple.com/cn/app/qu-dong/id1531067887?l=zh&ls=1"); } else { launch(info?.lastVersion?.downloadUrl ?? ""); } } } }