application.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:connectivity_plus/connectivity_plus.dart';
  5. import 'package:device_info/device_info.dart';
  6. import 'package:fluro/fluro.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:logger/logger.dart';
  9. import 'package:package_info/package_info.dart';
  10. import 'package:permission_handler/permission_handler.dart';
  11. const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  12. bool isDebugShoe = false;
  13. String shoeVersion = "";
  14. bool openSocial() {
  15. // return false;
  16. return isDebugShoe || Platform.isIOS;
  17. }
  18. final logger = Logger();
  19. final RouteObserver<ModalRoute> routeObserver = RouteObserver<ModalRoute>();
  20. Locale? locale;
  21. double weight = 60.0;
  22. int gender = 0;
  23. String? buyShoesUrl;
  24. AppLifecycleState? appLifecycleState = AppLifecycleState.resumed;
  25. ConnectivityResult? connectivityResult;
  26. class Application {
  27. static BuildContext? context;
  28. static FluroRouter? router;
  29. static Map<int, bool> gameLikes = {};
  30. static int currentPage = 0;
  31. static Map<String, dynamic> infos = {};
  32. static Future<String> getDeviceInfo() async {
  33. Map<String, dynamic> infos = await getDeviceInfoMap();
  34. return json.encode(infos);
  35. }
  36. static Future<Map<String, dynamic>> getDeviceInfoMap() async {
  37. if (infos.isNotEmpty) return infos;
  38. infos['os'] = Platform.isAndroid
  39. ? 1
  40. : Platform.isIOS
  41. ? 2
  42. : 3;
  43. PackageInfo packageInfo = await PackageInfo.fromPlatform();
  44. infos['versionCode'] = packageInfo.buildNumber;
  45. infos['versionName'] = packageInfo.version;
  46. infos['locale'] = locale?.languageCode ?? "";
  47. try {
  48. DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  49. if (Platform.isIOS) {
  50. print('IOS设备:');
  51. IosDeviceInfo info = await deviceInfo.iosInfo;
  52. infos['brand'] = info.model;
  53. infos['name'] = info.name;
  54. infos['model'] = info.utsname.machine;
  55. infos['identifierForVendor'] = info.identifierForVendor;
  56. infos['isPhysicalDevice'] = info.isPhysicalDevice;
  57. infos['systemName'] = info.systemName;
  58. infos['systemVersion'] = info.systemVersion;
  59. infos['utsname'] = info.utsname.sysname + "_" + info.utsname.nodename + "_" + info.utsname.machine + "_" + info.utsname.version + "_" + info.utsname.release;
  60. } else if (Platform.isAndroid) {
  61. print('Android设备');
  62. AndroidDeviceInfo info = await deviceInfo.androidInfo;
  63. infos['model'] = info.model;
  64. infos['isPhysicalDevice'] = info.isPhysicalDevice;
  65. //infos['id'] = info.id;
  66. //infos['type'] = info.type;
  67. // infos['tags'] = info.tags;
  68. infos['device'] = info.device;
  69. infos['androidId'] = info.androidId;
  70. infos['board'] = info.board;
  71. //infos['bootloader'] = info.bootloader;
  72. infos['brand'] = info.brand;
  73. //infos['display'] = info.display;
  74. //infos['fingerprint'] = info.fingerprint;
  75. infos['hardware'] = info.hardware;
  76. infos['manufacturer'] = info.manufacturer;
  77. infos['product'] = info.product;
  78. var version = info.version;
  79. infos['baseOS'] = version.baseOS;
  80. infos['codename'] = version.codename;
  81. infos['sdkInt'] = version.sdkInt;
  82. infos['release'] = version.release;
  83. }
  84. } catch (e) {
  85. print(e);
  86. }
  87. infos.forEach((key, value) {
  88. if(value is String)
  89. infos[key] = Uri.encodeComponent(value);
  90. });
  91. return infos;
  92. }
  93. static Future<bool> requestPermission(Permission permission) async {
  94. PermissionStatus _permissionStatus = await permission.status;
  95. if (_permissionStatus != PermissionStatus.granted) {
  96. // 没有权限,发起请求权限
  97. _permissionStatus = await permission.request();
  98. return _permissionStatus == PermissionStatus.granted;
  99. } else {
  100. return true;
  101. }
  102. }
  103. }