position_sensors 0.0.3 position_sensors: ^0.0.3 copied to clipboard
A plugin to access information about position sensors.
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:position_sensors/position_sensors.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<String> _sensors = [];
double _x = 0.0;
double _y = 0.0;
double _z = 0.0;
int _delay = -1;
double _distance = 0.0;
double _farValue = 0.0;
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
List<String> sensors;
try {
sensors = await PositionSensors.supportedSensors ?? [];
} on PlatformException {
sensors = [];
}
try {
await PositionSensors.setDelay(1);
} catch (_) {}
int delay;
try {
delay = await PositionSensors.delay ?? -1;
} on PlatformException {
delay = -1;
}
double farValue;
try {
farValue = await PositionSensors.proximityFarValue ?? -1.0;
} on PlatformException {
farValue = -1.0;
}
if (!mounted) return;
PositionSensors.gameRotationEvents.listen((e) {
setState(() {
double theta = acos(e.w) * 2;
double sinTheta2 = sin(theta / 2);
_x = (180 * (e.x / sinTheta2)) / pi;
_y = (180 * (e.y / sinTheta2)) / pi;
_z = (180 * (e.w / sinTheta2)) / pi;
});
});
PositionSensors.proximityEvents.listen((e) {
setState(() {
_distance = e.distance;
});
});
setState(() {
_sensors = sensors;
_delay = delay;
_farValue = farValue;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
Text('Sensors: $_sensors'),
Text('Delay: $_delay'),
Text('Rotation: (${_x.round()}°, '
'${_y.round()}°, '
'${_z.round()}°)'),
Text('Proximity: $_distance cm (max is $_farValue cm)'),
],
),
),
),
);
}
}