flutter:用device_info_plus得到设备信息

一,安装第三方库

库地址:

https://pub.dev/packages/device_info_plus

安装:编辑pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  device_info_plus: ^11.3.3

然后点击 pub get

二,代码:

1,助手类:

import 'package:flutter/material.dart';
import 'dart:io' show Platform;
import 'package:device_info_plus/device_info_plus.dart';

class SystemHelper {
  // 获取设备信息
  static Future<Map<dynamic, dynamic>> getDeviceInfo() async {
    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    BaseDeviceInfo deviceInfo = await deviceInfoPlugin.deviceInfo;
    //print(deviceInfo.data.entries);
    Map<dynamic, dynamic> maplist = {};
    //遍历得到map:
    for (var entry in deviceInfo.data.entries) {
      maplist[entry.key] = entry.value;
    }

    return maplist;
  }
}

2,页面:

import 'package:demo3/common/SystemHelper.dart';
import 'package:dio/io.dart';
import 'package:flutter/material.dart';
import 'package:image_gallery_saver_plus/image_gallery_saver_plus.dart';
import 'package:dio/dio.dart';
import 'dart:typed_data';

class SystemPage extends StatefulWidget {
  final Map arguments;

  // 为title设置一个默认参数,这样的跳转该界面时可以不传值。
  SystemPage({super.key, required this.arguments});
  @override
  State<SystemPage> createState() => _SystemPageState();
}

class _SystemPageState extends State<SystemPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.primaryContainer,
        title: Text(widget.arguments["title"]),
      ),

      body: Center(
        child:Column(
            children: [
            ElevatedButton(
            onPressed: () async {
              Map<dynamic, dynamic> info = await SystemHelper.getDeviceInfo();
              print("当前设备信息:");
              for (var entry in info.entries) {
                print('${entry.key}: ${entry.value}');
              }

              if (info.containsKey('version') && info["version"].containsKey('sdkInt')) {
                   var sdkInt = info["version"]["sdkInt"];
                   print("键sdkInt存在:");
                   print(sdkInt);
              } else {
                   print('键 "sdkInt" 不存在');
              }



            },
            child:  Row(
                mainAxisSize: MainAxisSize.min, // 根据内容调整大小
                children: <Widget>[
                  Icon(Icons.add), // 图标在左侧
                  SizedBox(width: 10), // 可选:添加一些间隔
                  Text("得到系统信息"), // 文本在右侧
                ],
              ),
            ),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  //Image.file(File(imageData), width: 600.0, height: 290.0)
                ],
              ),
            ],
          ),

        ),
      );
  }
}

三,测试效果:

I/flutter ( 5757): 当前设备信息:
I/flutter ( 5757): product: sdk_gphone_x86_arm
I/flutter ( 5757): supportedAbis: [x86, armeabi-v7a, armeabi]
I/flutter ( 5757): serialNumber: unknown
I/flutter ( 5757): supported32BitAbis: [x86, armeabi-v7a, armeabi]
I/flutter ( 5757): display: PSR1.180720.117
I/flutter ( 5757): type: user
I/flutter ( 5757): isPhysicalDevice: false
I/flutter ( 5757): version: {baseOS: , securityPatch: 2019-08-05, sdkInt: 28, release: 9, codename: REL, previewSdkInt: 0, incremental: 5875966}
I/flutter ( 5757): systemFeatures: [android.hardware.sensor.proximity, android.software.adoptable_storage, android.hardware.sensor.accelerometer, android.hardware.faketouch, android.software.backup, android.hardware.touchscreen, android.hardware.touchscreen.multitouch, android.software.print, android.software.voice_recognizers, android.software.picture_in_picture, android.hardware.fingerprint, android.hardware.sensor.gyroscope, android.software.cant_save_state, android.hardware.sensor.relative_humidity, android.hardware.camera.autofocus, com.google.android.feature.GOOGLE_BUILD, android.hardware.telephony.gsm, android.hardware.audio.output, android.software.verified_boot, android.hardware.camera.front, android.hardware.screen.portrait, android.hardware.sensor.ambient_temperature, android.software.home_screen, android.hardware.microphone, android.software.autofill, android.hardware.sensor.compass, android.hardware.touchscreen.multitouch.jazzhand, android.hardware.sensor.barometer, android.software.app_widgets, android.software.i
I/flutter ( 5757): manufacturer: Google
I/flutter ( 5757): tags: release-keys
I/flutter ( 5757): supported64BitAbis: []
I/flutter ( 5757): bootloader: unknown
I/flutter ( 5757): fingerprint: google/sdk_gphone_x86_arm/generic_x86_arm:9/PSR1.180720.117/5875966:user/release-keys
I/flutter ( 5757): host: abfarm-us-east1-c-0025
I/flutter ( 5757): name: AOSP on IA Emulator
I/flutter ( 5757): isLowRamDevice: false
I/flutter ( 5757): model: AOSP on IA Emulator
I/flutter ( 5757): id: PSR1.180720.117
I/flutter ( 5757): brand: google
I/flutter ( 5757): device: generic_x86_arm
I/flutter ( 5757): board: goldfish_x86
I/flutter ( 5757): hardware: ranchu
I/flutter ( 5757): sdkInt:
I/flutter ( 5757): 28

 

posted @ 2025-04-04 13:14  刘宏缔的架构森林  阅读(361)  评论(0)    收藏  举报