Flutter实战-获取设备信息

Flutter实战-获取设备信息

  • 一. 前言
  • 二. 配置
    • 1.添加依赖
    • 2.配置IOS
  • 三. 代码步骤
    • 1.获取平台信息
    • 2.获取设备信息
  • 四. 总结

一. 前言

Flutter实战—获取设备信息。

二. 配置

1.添加依赖

device_info:Get current device information from within the Flutter application.
版本查询 : https://pub.dev/packages/device_info

1.1 在项目的pubspec.yaml文件中添加依赖:device_info

environment:
 	sdk: ">=2.1.0 <3.0.0"
  
dependencies:
	flutter:
	    sdk: flutter
	//添加依赖
  	device_info: 0.4.0+2

1.2 安装下载依赖包

flutter packages get

2.配置IOS

如果需要在IOS运行,需要安装cocoapods和brew:

brew install cocoapods
pod setup

三. 代码步骤

1.获取平台信息

Flutter是全平台框架,可以在多种平台上运行,获取设备信息之前要先判断程序运行在哪种平台。
判断平台:通过dart.io包下的PlatformPlatform类来获取。
1.1 Platform类

class Platform {
...
  static final bool isLinux = (_operatingSystem == "linux");
  static final bool isMacOS = (_operatingSystem == "macos");
  static final bool isWindows = (_operatingSystem == "windows");
  static final bool isAndroid = (_operatingSystem == "android");
  static final bool isIOS = (_operatingSystem == "ios");
  static final bool isFuchsia = (_operatingSystem == "fuchsia");
...
}

1.2 示例
(1)导入dart.io

import 'dart:io';

(2)示例—判断平台(IOS/Android)

if(Platform.isIOS){
  print('This is IOS.');
}else if(Platform.isAndroid)
  print('This is Android.');
}

2.获取设备信息

2.1导入device_info

import 'package:device_info/device_info.dart';

2.2 获取设备信息(IOS/Android)

void getDeviceInfo() async{
    DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
    if(Platform.isIOS){
      IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
    }else if(Platform.isAndroid){
      AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
    }
  }

2.3 IosDeviceInfo类
(1) 获取具体属性值:iosDeviceInfo.属性名(String name = iosDeviceInfo.name)

class IosDeviceInfo {
...
  final String name;
  final String systemName;
  final String systemVersion;
  final String model;
  final String localizedModel;
  final String identifierForVendor;
  final bool isPhysicalDevice;
  final IosUtsname utsname;
...
}

2.4 AndroidDeviceInfo类
(1) 获取具体属性值:androidDeviceInfo.属性名(String brand = androidDeviceInfo.brand)

class AndroidDeviceInfo {
...
  final AndroidBuildVersion version;
  final String board;
  final String bootloader;
  final String brand;
  final String device;
  final String display;
  final String fingerprint;
  final String hardware;
  final String host;
  final String id;
  final String manufacturer;
  final String model;
  final String product;
  final List<String> supported32BitAbis;
  final List<String> supported64BitAbis;
  final List<String> supportedAbis;
  final String tags;
  final String type;
  final bool isPhysicalDevice;
  final String androidId;
...
}

(2) AndroidBuildVersion类:androidDeviceInfo.version

class AndroidBuildVersion {
...
  final String baseOS;
  final String codename;
  final String incremental;
  final int previewSdkInt;
  final String release;
  final int sdkInt;
  final String securityPatch;
...
}

四. 总结

发布此文,目的只是为学习到的知识点进行记录,如有错误请多多指点。

你可能感兴趣的:(flutter实战)