From 38fc9b0830ce5e08e4e7ede12303900748dfec00 Mon Sep 17 00:00:00 2001 From: TongTongStudio Date: Sun, 14 Jun 2026 13:10:32 +0800 Subject: [PATCH] feat: add device_info_plus --- android/gradle.properties | 6 +- lib/main.dart | 7 +++ lib/utils/DeviceInfoUtil.dart | 58 ++++++++++++++++++ macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.lock | 61 ++++++++++++++++--- pubspec.yaml | 1 + 6 files changed, 126 insertions(+), 9 deletions(-) diff --git a/android/gradle.properties b/android/gradle.properties index 24863d2..475a628 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,3 +1,7 @@ org.gradle.jvmargs=-Xmx8G -XX:MaxMetaspaceSize=4G -XX:ReservedCodeCacheSize=512m -XX:+HeapDumpOnOutOfMemoryError android.useAndroidX=true -android.enableJetifier=true \ No newline at end of file +android.enableJetifier=true +# This builtInKotlin flag was added automatically by Flutter migrator +android.builtInKotlin=false +# This newDsl flag was added automatically by Flutter migrator +android.newDsl=false diff --git a/lib/main.dart b/lib/main.dart index d569da7..c860cef 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:device_info_plus/device_info_plus.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/scheduler.dart'; import 'package:get/get.dart'; @@ -13,6 +14,12 @@ void main() async{ // must wait for MMKV to finish initialization final rootDir = await MMKV.initialize(); print('MMKV for flutter with rootDir = $rootDir'); + + final deviceInfoPlugin = DeviceInfoPlugin(); + final deviceInfo = await deviceInfoPlugin.deviceInfo; + final allInfo = deviceInfo.data; + print('Device info: $allInfo'); + runApp(const MyApp()); } diff --git a/lib/utils/DeviceInfoUtil.dart b/lib/utils/DeviceInfoUtil.dart index e69de29..5838b73 100644 --- a/lib/utils/DeviceInfoUtil.dart +++ b/lib/utils/DeviceInfoUtil.dart @@ -0,0 +1,58 @@ +import 'package:device_info_plus/device_info_plus.dart'; +import 'package:flutter/foundation.dart'; + +class DeviceInfoUtil { + static final DeviceInfoPlugin _deviceInfo = DeviceInfoPlugin(); + + /// 获取设备信息 + static Future printDeviceInfo() async { + try { + if (defaultTargetPlatform == TargetPlatform.android) { + await _printAndroidDeviceInfo(); + } else if (defaultTargetPlatform == TargetPlatform.iOS) { + await _printIosDeviceInfo(); + } else { + debugPrint('当前平台不支持: $defaultTargetPlatform'); + } + } catch (e) { + debugPrint('获取设备信息失败: $e'); + } + } + + /// 获取 Android 设备信息 + static Future _printAndroidDeviceInfo() async { + final androidInfo = await _deviceInfo.androidInfo; + + debugPrint('========== Android 设备信息 =========='); + debugPrint('品牌: ${androidInfo.brand}'); + debugPrint('制造商: ${androidInfo.manufacturer}'); + debugPrint('机型: ${androidInfo.model}'); + debugPrint('设备名称: ${androidInfo.device}'); + debugPrint('产品名: ${androidInfo.product}'); + debugPrint('硬件名: ${androidInfo.hardware}'); + debugPrint('主板: ${androidInfo.board}'); + debugPrint('序列号: ${androidInfo.serialNumber}'); + debugPrint('Android ID: ${androidInfo.id}'); + debugPrint('系统版本: ${androidInfo.version.release}'); + debugPrint('SDK 版本: ${androidInfo.version.sdkInt}'); + debugPrint('安全补丁: ${androidInfo.version.securityPatch}'); + debugPrint('API 级别: ${androidInfo.version.codename}'); + debugPrint('设备类型: ${androidInfo.isPhysicalDevice ? "物理设备" : "模拟器"}'); + debugPrint('====================================='); + } + + /// 获取 iOS 设备信息 + static Future _printIosDeviceInfo() async { + final iosInfo = await _deviceInfo.iosInfo; + + debugPrint('========== iOS 设备信息 =========='); + debugPrint('设备名称: ${iosInfo.name}'); + debugPrint('系统名称: ${iosInfo.systemName}'); + debugPrint('系统版本: ${iosInfo.systemVersion}'); + debugPrint('机型: ${iosInfo.model}'); + debugPrint('本地化机型: ${iosInfo.localizedModel}'); + debugPrint('标识符: ${iosInfo.identifierForVendor}'); + debugPrint('是否是物理设备: ${iosInfo.isPhysicalDevice ? "是" : "否"}'); + debugPrint('================================='); + } +} diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 0718498..1fe62c5 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,8 +5,10 @@ import FlutterMacOS import Foundation +import device_info_plus import mmkv_ios func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin")) MMKVPlugin.register(with: registry.registrar(forPlugin: "MMKVPlugin")) } diff --git a/pubspec.lock b/pubspec.lock index dd6aaef..570df38 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -73,6 +73,22 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "1.0.9" + device_info_plus: + dependency: "direct main" + description: + name: device_info_plus + sha256: "6a642e1daa10190af89ba6cb6386c0df7d071a3592080bfe1e44faa63ae1df65" + url: "https://pub.flutter-io.cn" + source: hosted + version: "13.1.0" + device_info_plus_platform_interface: + dependency: transitive + description: + name: device_info_plus_platform_interface + sha256: "04b173a92e2d9161dfead145667037c8d834db725ce2e7b942bfe18fd2f45a46" + url: "https://pub.flutter-io.cn" + source: hosted + version: "8.1.0" dio: dependency: "direct main" description: @@ -105,6 +121,14 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "2.2.0" + ffi_leak_tracker: + dependency: transitive + description: + name: ffi_leak_tracker + sha256: "4093d4ef9ca06ffe2786e73bfb25e22aa92112b9bb4ec941f11e3e6b61489a97" + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.1.2" file: dependency: transitive description: @@ -122,18 +146,18 @@ packages: dependency: transitive description: name: flutter_baidu_mapapi_base - sha256: "69578d884cf110a92d6e8dcdb24732474e48649727d3e9396f1919cf6ce3fae6" + sha256: "5a85ad1df38d2614dd556d7fd9de2e28ffc4890a89fc9e3a4b3a311304b5bad8" url: "https://pub.flutter-io.cn" source: hosted - version: "4.0.0" + version: "3.9.9+1" flutter_baidu_mapapi_map: dependency: "direct main" description: name: flutter_baidu_mapapi_map - sha256: "18d98c44a06da755093bc6069912a96d9ce6919a768a6aa16c9d4372e7837399" + sha256: "74f72140ed92368559434fcaf423eb5d8a466c1261229633fab50d4f01da4b01" url: "https://pub.flutter-io.cn" source: hosted - version: "4.0.0" + version: "3.9.9+1" flutter_lints: dependency: "direct dev" description: @@ -155,6 +179,11 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" get: dependency: "direct main" description: @@ -263,10 +292,10 @@ packages: dependency: transitive description: name: meta - sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394" + sha256: "1741988757a65eb6b36abe716829688cf01910bbf91c34354ff7ec1c3de2b349" url: "https://pub.flutter-io.cn" source: hosted - version: "1.17.0" + version: "1.18.0" mime: dependency: transitive description: @@ -492,10 +521,10 @@ packages: dependency: transitive description: name: test_api - sha256: "8161c84903fd860b26bfdefb7963b3f0b68fee7adea0f59ef805ecca346f0c7a" + sha256: "949a932224383300f01be9221c39180316445ecb8e7547f70a41a35bf421fb9e" url: "https://pub.flutter-io.cn" source: hosted - version: "0.7.10" + version: "0.7.11" typed_data: dependency: transitive description: @@ -528,6 +557,22 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "1.1.1" + win32: + dependency: transitive + description: + name: win32 + sha256: a1fc9eb9248baa05dfc12ed5b66e377b3e23f095eec078e0371622b9033810d9 + url: "https://pub.flutter-io.cn" + source: hosted + version: "6.2.0" + win32_registry: + dependency: transitive + description: + name: win32_registry + sha256: "73b1d78920a9d6e03f8b4e43e612b87bf3152a0e5c5e5150267762b7c4116904" + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.0.3" xdg_directories: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 425aff5..67aed76 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,6 +34,7 @@ dependencies: dio: ^5.9.2 mmkv: ^2.4.0 flutter_baidu_mapapi_map: ^3.9.9 + device_info_plus: ^13.1.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.