美文网首页
flutter 获取设备信息

flutter 获取设备信息

作者: 苦咖啡Li | 来源:发表于2020-12-15 18:56 被阅读0次

1、 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;

2、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;

3、 Platform 类下的代码 (参考文章: https://www.jianshu.com/p/ec401fb90c0e)

    /**
    * Whether the operating system is a version of
    * [Linux](https://en.wikipedia.org/wiki/Linux).
    *
    * This value is `false` if the operating system is a specialized
    * version of Linux that identifies itself by a different name,
    * for example Android (see [isAndroid]).
    */
    static final bool isLinux = (_operatingSystem == "linux");

    /**
    * Whether the operating system is a version of
    * [macOS](https://en.wikipedia.org/wiki/MacOS).
    */
    static final bool isMacOS = (_operatingSystem == "macos");

    /**
    * Whether the operating system is a version of
    * [Microsoft Windows](https://en.wikipedia.org/wiki/Microsoft_Windows).
    */
    static final bool isWindows = (_operatingSystem == "windows");

    /**
    * Whether the operating system is a version of
    * [Android](https://en.wikipedia.org/wiki/Android_%28operating_system%29).
    */
    static final bool isAndroid = (_operatingSystem == "android");

    /**
    * Whether the operating system is a version of
    * [iOS](https://en.wikipedia.org/wiki/IOS).
    */
    static final bool isIOS = (_operatingSystem == "ios");

    /**
    * Whether the operating system is a version of
    * [Fuchsia](https://en.wikipedia.org/wiki/Google_Fuchsia).
    */
    static final bool isFuchsia = (_operatingSystem == "fuchsia");

4、项目中获取设备信息

4.1 pubspec.yaml 中添加依赖包
    device_info: ^0.4.2+4
4.2 创建 device_info.dart 文件,封装获取设备信息的方法
    /*
    * @description:  获取设备信息
    * @return {type} 设备信息
    */
    Future<dynamic> getDeviceInfo() async{
        DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
        var dataInfo;
        if(Platform.isIOS){
            print('IOS设备:');
            IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
            dataInfo = iosInfo;
        }else if(Platform.isAndroid){
            print('Android设备');
            AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
            dataInfo = androidInfo;
        }
        return dataInfo;
    }

    //  获取设备的指定信息
    class FlutterDeviceInfo{
        // 获取设备的唯一标识 uuid
        static Future<String> get platformUid async {
            var data = await getDeviceInfo(), res;
            if(Platform.isIOS){
                res = data.identifierForVendor;
            }else if(Platform.isAndroid){
                res = data.id;
            }
            return res;
        }
        
        //  获取设备name 
        static Future<String> get platformName async {
            var data = await getDeviceInfo(), res;
            if(Platform.isIOS){
                res = data.name;
            }else if(Platform.isAndroid){
                res = data.device;
            }
            return res;
        }

        // 获取设备的model
        static Future<String> get platformModel async {
            var data = await getDeviceInfo();
            return data.model;
        }
    }
4.3 项目组件中使用
    // 引入依赖包
    import '../utils/device_info.dart';

    // 组件中使用
    RaisedButton(
        child: Text("获取设备信息"),
        onPressed: ()async{
            var platformUid = await FlutterDeviceInfo.platformUid;
            var platformName = await FlutterDeviceInfo.platformName;
            var platformModel = await FlutterDeviceInfo.platformModel;
            print( ' platformUid: $platformUid , platformName: $platformName , platformModel: $platformModel');
            // 模拟器输出结果:  platformUid: RPB2.200611.009 , platformName: generic_x86_64_arm64 , platformModel: sdk_gphone_x86_64
        }
    )

相关文章

网友评论

      本文标题:flutter 获取设备信息

      本文链接:https://www.haomeiwen.com/subject/atqluktx.html