circle_app/circle_app/lib/util/device.dart
2023-08-15 16:48:14 +08:00

127 lines
3.5 KiB
Dart

import 'dart:io';
import 'package:device_info/device_info.dart';
import 'package:geolocator/geolocator.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'SharedPreferencesHelper.dart';
//手机系统版本
Future<String> getDeviceId() async {
String deviceId = "";
final DeviceInfoPlugin deviceInfoPlugin = new DeviceInfoPlugin();
try {
if (Platform.isAndroid) {
var build = await deviceInfoPlugin.androidInfo;
String version = build.version.release;
deviceId = version; // Android
} else if (Platform.isIOS) {
var data = await deviceInfoPlugin.iosInfo;
String version = data.systemVersion;
deviceId = version; // iOS
}
} catch (e) {
print('Failed to get device id: $e');
}
return deviceId;
}
Future<String> getVersion() async {
String version = "";
final info = await PackageInfo.fromPlatform();
version = info.version;
return version;
}
Future<String> getVersionName() async {
String versionName = "";
final info = await PackageInfo.fromPlatform();
versionName = info.version;
return versionName;
}
Future<String> getVersionCode() async {
String versionCode = "";
final info = await PackageInfo.fromPlatform();
versionCode = info.buildNumber;
return versionCode;
}
//imei
Future<String> getImei() async {
String imei = "";
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
imei = androidInfo.androidId;
// other specific Android data
} else if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
imei = iosInfo.identifierForVendor;
// other specific iOS data
}
return imei;
}
//手机型号
Future<String> getBrand() async {
String brand = '';
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isIOS) {
IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
brand = iosDeviceInfo.model;
} else if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
brand = androidInfo.model;
}
return brand;
}
Future<LatLng> getLocation() async {
LatLng latLng = LatLng(latitude: 0.0, longitude: 0.0);
;
try {
LocationPermission permission = await Geolocator.requestPermission();
if (permission == LocationPermission.whileInUse ||
permission == LocationPermission.always) {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
);
print('Latitude: ${position.latitude}');
print('Longitude: ${position.longitude}');
latLng =
LatLng(latitude: position.latitude, longitude: position.longitude);
} else {
print('Location permission denied');
}
} catch (e) {
print('Failed to get device or location info: $e');
}
return latLng;
}
class LatLng {
final double latitude;
final double longitude;
LatLng({required this.latitude, required this.longitude});
}
Future<String> getAuthorization() async {
String token ="";
SharedPreferences sharedPreferences =await SharedPreferences.getInstance();
token = sharedPreferences.getString(SharedPreferencesHelper.AUTHORIZATION)??"";
return token;
}
Future<bool> getAgreemement() async {
if(Platform.isIOS){
return true;
}
bool isAgreemement =false;
SharedPreferences sharedPreferences =await SharedPreferences.getInstance();
isAgreemement = sharedPreferences.getBool(SharedPreferencesHelper.AGREEMENT)??false;
return isAgreemement;
}