update_apk.dart 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import 'dart:io';
  2. import 'package:device_info/device_info.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get_it/get_it.dart';
  5. import 'package:package_info/package_info.dart';
  6. import 'package:shared_preferences/shared_preferences.dart';
  7. import 'package:sport/bean/UpdateInfo.dart';
  8. import 'package:sport/services/api/resp.dart';
  9. import 'package:sport/services/api/rest_client.dart';
  10. import 'package:sport/utils/toast.dart';
  11. import 'package:sport/utils/version.dart';
  12. import 'package:sport/widgets/dialog/alert_dialog.dart';
  13. import 'package:url_launcher/url_launcher.dart';
  14. updateApp(BuildContext context) async {
  15. updateApk(context);
  16. }
  17. Future<RespData<UpdateInfo>> checkUpdate() async {
  18. String version = (await PackageInfo.fromPlatform()).version;
  19. final api = GetIt.I<RestClient>();
  20. RespData<UpdateInfo> data = await api.checkUpdate(version).catchError((err) {});
  21. return data;
  22. }
  23. updateApk(BuildContext context, {bool tips = false}) async {
  24. SharedPreferences prefs = await SharedPreferences.getInstance();
  25. String? token = prefs.getString("token");
  26. if (token?.isEmpty == true) return;
  27. RespData<UpdateInfo> data = await checkUpdate();
  28. if (data.code == 0) {
  29. UpdateInfo? info = data.data;
  30. if (info?.needUpdate == false) {
  31. if (tips == true) {
  32. ToastUtil.show("当前已是最新版本");
  33. }
  34. return;
  35. }
  36. if (info?.lastVersion == null) return;
  37. try {
  38. DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  39. if (Platform.isAndroid) {
  40. AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
  41. AndroidBuildVersion buildVersion = androidDeviceInfo.version;
  42. if (buildVersion.sdkInt < int.parse(info?.lastVersion?.minVer ?? "0")) {
  43. return;
  44. }
  45. } else if (Platform.isIOS) {
  46. IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
  47. if (versionCompare(iosDeviceInfo.systemVersion, info?.lastVersion?.minVer ?? "0") < 0) {
  48. return;
  49. }
  50. }
  51. } catch (e) {
  52. print(e);
  53. }
  54. var result = await showDialog(
  55. context: context,
  56. builder: (context) => CustomAlertDialog(
  57. title: '发现新版本 ${info?.lastVersion?.version}',
  58. child: Container(
  59. width: double.infinity,
  60. padding: const EdgeInsets.symmetric(horizontal: 24.0),
  61. child: Text(
  62. "${info?.lastVersion?.detail?.replaceAll("\\n", "\n")}",
  63. style: TextStyle(fontSize: 14, color: Color(0xff333333), height: 1.4),
  64. )),
  65. textOk: '立即更新',
  66. ok: () => Navigator.of(context).pop(true)),
  67. );
  68. if (result == true) {
  69. if (Platform.isAndroid) {
  70. launch(info?.lastVersion?.downloadUrl ?? "");
  71. } else if (Platform.isIOS) {
  72. launch("https://apps.apple.com/cn/app/qu-dong/id1531067887?l=zh&ls=1");
  73. } else {
  74. launch(info?.lastVersion?.downloadUrl ?? "");
  75. }
  76. }
  77. }
  78. }