Flutter 获取设备信息
// 来源链接为 device_info 插件在 pub.dev 的官方页面
https://pub.dev/packages/device_info
// 导入 Flutter 的 Material 设计组件和 device_info 插件
import 'package:flutter/material.dart';
import 'package:device_info/device_info.dart';
// 主页面
class DevicPage extends StatefulWidget{
DevicPage({Key key});
_DevicPage createState() => _DevicPage();
}
// 状态管理
class _DevicPage extends State {
var _text;
// 初始化状态
@override
initState() {
super.initState();
_getDevice();
}
// 获取设备信息
_getDevice() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('设备信息 ${androidInfo}');
_text = androidInfo;
}
// 构建页面
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('device'),),
body: ListView(
children: <Widget>[
// 设备的各种信息
Text('${_text.version}'),
Text('${_text.board}'),
...
Text('${_text.androidId}'),
],
)
);
}
}
Flutter 使用高德定位前的准备工作
// 1. 首先需要申请成为开发者
// 2. 然后创建应用配置以获取 Key,可参考官方教程
https://lbs.amap.com/api/android-sdk/guide/create-project/get-key
Flutter 使用高德定位
// 高德定位插件在 pub.dev 的官方页面
https://pub.dev/packages/amap_location
// 配置高德的 key,注意,需要添加到 android 项目中,而不是外层
manifestPlaceholders = [AMAP_KEY : "ad2d13e8fab8a607b4707a598e32fc70"] // 使用自己的高德key
dependencies {
// 注意,需要在主项目中添加此依赖,否则可能导致编译失败
implementation 'com.amap.api:location:latest.integration'
}
// 导入 Flutter 的 Material 设计组件和 amap_location 插件
import 'package:flutter/material.dart';
import 'package:amap_location/amap_location.dart';
// 主页面
class GpsPage extends StatefulWidget{
GpsPage({Key key});
_GpsPage createState() => _GpsPage();
}
// 状态管理
class _GpsPage extends State {
var gps;
// 初始化状态
@override
initState() {
super.initState();
_getGps();
}
// 获取定位信息
_getGps() async {
var reult = await AMapLocationClient.startup(new AMapLocationOption( desiredAccuracy:CLLocationAccuracy.kCLLocationAccuracyHundredMeters ));
var res = await AMapLocationClient.getLocation(true);
setState(() {
gps = res;
});
}
// 构建页面
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('device'),),
body: ListView(
children: <Widget>[
// 显示定位的经度、纬度、精度
Text('${gps.longitude}'),
Text('${gps.latitude}'),
Text('${gps.accuracy}'),
],
)
);
}
}