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

首先需要依赖device_info:device_info | Flutter Package

然后执行:flutter packages get下载依赖,在需要用到的地方引入依赖即可,下面上代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:device_info/device_info.dart';

class TestPage extends StatefulWidget {
  final arguments;
  TestPage({Key key, this.arguments}) : super(key : key);
  _TestPageState createState() => _TestPageState(this.arguments);
}

class _TestPageState extends State<TestPage> {
  final arguments;
  _TestPageState(this.arguments);

  @override
  void initState() {
    _judgePlatformInfo();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  _judgePlatformInfo () async {
    DeviceInfoPlugin dip = new DeviceInfoPlugin();
    //判断设备信息:安卓或ios
    if (Platform.isAndroid) {
      AndroidDeviceInfo adi = await dip.androidInfo;
      xxx
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await dip.iosInfo;
      xxx
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("测试"),
      ),
      body: Center(
        child: Text("测试"),
      ),
    );
  }
}

搞定,里面的API大家自行查看吧,都打印一下看看都是什么,印象深刻~

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>