update_apk.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import 'dart:io';
  2. import 'package:dartin/dartin.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:package_info/package_info.dart';
  5. import 'package:shared_preferences/shared_preferences.dart';
  6. import 'package:sport/bean/UpdateInfo.dart';
  7. import 'package:sport/services/api/resp.dart';
  8. import 'package:sport/services/api/rest_client.dart';
  9. import 'package:sport/widgets/dialog/alert_dialog.dart';
  10. import 'package:url_launcher/url_launcher.dart';
  11. updateApp(BuildContext context) async {
  12. if (Platform.isAndroid) {
  13. updateApk(context);
  14. }
  15. }
  16. updateApk(BuildContext context) async {
  17. SharedPreferences prefs = await SharedPreferences.getInstance();
  18. String token = prefs.getString("token");
  19. if (token?.isEmpty == true) return;
  20. String version = (await PackageInfo.fromPlatform()).version;
  21. final api = inject<RestClient>();
  22. RespData<UpdateInfo> data = await api.checkUpdate(version).catchError((err) {});
  23. if (data.code == 0) {
  24. UpdateInfo info = data.data;
  25. if (!info.needUpdate) {
  26. return;
  27. }
  28. if (await showDialog(
  29. context: context,
  30. builder: (context) => CustomAlertDialog(
  31. title: '发现新版本 ${info.lastVersion.version}',
  32. child: Container(
  33. width: double.infinity,
  34. padding: const EdgeInsets.symmetric(horizontal: 24.0),
  35. child: Text(
  36. "${info.lastVersion.detail}",
  37. style: TextStyle(fontSize: 14, color: Color(0xff333333), height: 1.4),
  38. )),
  39. textOk: '立即更新',
  40. ok: () => Navigator.of(context).pop(true)),
  41. ) ==
  42. true) {
  43. launch(info.lastVersion.downloadUrl);
  44. }
  45. }
  46. }