Flutter中使用device_info获取设备信息

1. 安装插件

配置 device_info 插件。

dependencies:
  flutter:
    sdk: flutter

  # 设备信息
  device_info: ^2.0.2

在pubspec.yaml中配置保存后,在开发工具环境中会自动下载依赖包。

如果无法正常下载,执行 flutter pub get 。

2. 引入依赖

在需要用到的该插件的文件中引入插件包。

// 引入插件
import 'package:device_info/device_info.dart';

3. 使用插件

苹果设备:

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print('设备唯一标识:${iosInfo.identifierForVendor}'); 
// 更多信息请查看 AndroidDeviceInfo 类中的定义

安卓设备:

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('设备唯一标识: ${androidInfo.androidId}');
// 更多信息请查看 IosDeviceInfo 类中的定义

4. 完整示例

import 'package:flutter/material.dart';

// 引入插件
import 'package:device_info/device_info.dart';


class DevicePage extends StatefulWidget {

    DevicePage({Key key}) : super(key: key);

    @override
    _DevicePageState createState() => _DevicePageState();
}

class _DevicePageState extends State<DevicePage> {

    @override
    void initState() {
        super.initState();
        // 获取设备信息
        this._getDeviceInfo();
    }

    void _getDeviceInfo() async{
    
        DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

        // 安卓系统
        // AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
        // print('设备唯一标识: ${androidInfo.androidId}');
        // 更多信息请查看 AndroidDeviceInfo 类中的定义

        // 苹果系统
        IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
        print('设备唯一标识:${iosInfo.identifierForVendor}'); 
        // 更多信息请查看 IosDeviceInfo 类中的定义
    }


    @override
    Widget build(BuildContext context) {
        return Container(
            child: Scaffold(
                appBar: AppBar(
                    title: Text("设备信息"),
                ),
            )
        );
    }
}

参考:https://pub.flutter-io.cn/packages/device_info

Flutter 获取设备信息
获取当前设备平台
获取屏幕宽高
获取状态栏高度
获取当前设备平台
第一种,直接通过Platform类来判断,不需要任何参数,很方便

Platform.isAndroid;
Platform.isFuchsia;
Platform.isIOS;
Platform.isLinux;
Platform.isMacOS;

第二种,通过ThemeData拿到TargetPlatform,然后在判断也可以

final ThemeData theme = Theme.of(context);
TargetPlatform targetPlatform = theme.platform;
switch(targetPlatform){
  case TargetPlatform.android:
    break;
  case TargetPlatform.fuchsia:
    // TODO: Handle this case.
    break;
  case TargetPlatform.iOS:
    // TODO: Handle this case.
    break;
}

获取屏幕宽高
  ///屏幕size
  static Size screenSize(BuildContext context) {
    return MediaQuery.of(context).size;
  }

  ///   屏幕宽度
  static double screenWidth(BuildContext context) {
    return MediaQuery.of(context).size.width;
  }

  ///  屏幕高度
  static double screenHeight(BuildContext context) {
    return MediaQuery.of(context).size.height;
  }

获取状态栏高度
MediaQuery.of(context).padding.top
————————————————
版权声明:本文为CSDN博主「AnyEnter」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhuqian886/article/details/109111999

2、获取信息

android版本所有属性

  /// Android operating system version values derived from `android.os.Build.VERSION`.
  final AndroidBuildVersion version;

  /// The name of the underlying board, like "goldfish".
  final String board;

  /// The system bootloader version number.
  final String bootloader;

  /// The consumer-visible brand with which the product/hardware will be associated, if any.
  final String brand;

  /// The name of the industrial design.
  final String device;

  /// A build ID string meant for displaying to the user.
  final String display;

  /// A string that uniquely identifies this build.
  final String fingerprint;

  /// The name of the hardware (from the kernel command line or /proc).
  final String hardware;

  /// Hostname.
  final String host;

  /// Either a changelist number, or a label like "M4-rc20".
  final String id;

  /// The manufacturer of the product/hardware.
  final String manufacturer;

  /// The end-user-visible name for the end product.
  final String model;

  /// The name of the overall product.
  final String product;

  /// An ordered list of 32 bit ABIs supported by this device.
  final List<String> supported32BitAbis;

  /// An ordered list of 64 bit ABIs supported by this device.
  final List<String> supported64BitAbis;

  /// An ordered list of ABIs supported by this device.
  final List<String> supportedAbis;

  /// Comma-separated tags describing the build, like "unsigned,debug".
  final String tags;

  /// The type of build, like "user" or "eng".
  final String type;

  /// `false` if the application is running in an emulator, `true` otherwise.
  final bool isPhysicalDevice;

ios版本所有属性:

/// Device name.
  final String name;

  /// The name of the current operating system.
  final String systemName;

  /// The current operating system version.
  final String systemVersion;

  /// Device model.
  final String model;

  /// Localized name of the device model.
  final String localizedModel;

  /// Unique UUID value identifying the current device.
  final String identifierForVendor;

  /// `false` if the application is running in a simulator, `true` otherwise.
  final bool isPhysicalDevice;

  /// Operating system information derived from `sys/utsname.h`.
  final IosUtsname utsname;

好吧,只要一行依赖就可以集成两个平台的项目代码,毫无繁琐配置,flutter真的简单。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值