all_sensors 0.4.2 all_sensors: ^0.4.2 copied to clipboard
A Flutter plugin to retreive Accelerometer, Gyroscope, Proximity sensor's data.
import 'dart:async';
import 'package:all_sensors/all_sensors.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sensors Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<double> _accelerometerValues = <double>[];
List<double> _userAccelerometerValues = <double>[];
List<double> _gyroscopeValues = <double>[];
bool _proximityValues = false;
List<StreamSubscription<dynamic>> _streamSubscriptions = <StreamSubscription<dynamic>>[];
@override
Widget build(BuildContext context) {
final List<String>? accelerometer =
_accelerometerValues.map((double v) => v.toStringAsFixed(1)).toList();
final List<String>? gyroscope =
_gyroscopeValues.map((double v) => v.toStringAsFixed(1)).toList();
final List<String>? userAccelerometer =
_userAccelerometerValues.map((double v) => v.toStringAsFixed(1)).toList();
return Scaffold(
appBar: AppBar(
title: const Text('Sensor Example'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Padding(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Accelerometer: $accelerometer'),
],
),
padding: const EdgeInsets.all(16.0),
),
Padding(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('UserAccelerometer: $userAccelerometer'),
],
),
padding: const EdgeInsets.all(16.0),
),
Padding(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Gyroscope: $gyroscope'),
],
),
padding: const EdgeInsets.all(16.0),
),
Padding(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Proximity: $_proximityValues'),
],
),
padding: const EdgeInsets.all(16.0),
),
],
),
);
}
@override
void dispose() {
super.dispose();
for (StreamSubscription<dynamic> subscription in _streamSubscriptions) {
subscription.cancel();
}
}
@override
void initState() {
super.initState();
_streamSubscriptions.add(accelerometerEvents!.listen((AccelerometerEvent event) {
setState(() {
_accelerometerValues = <double>[event.x, event.y, event.z];
});
}));
_streamSubscriptions.add(gyroscopeEvents!.listen((GyroscopeEvent event) {
setState(() {
_gyroscopeValues = <double>[event.x, event.y, event.z];
});
}));
_streamSubscriptions.add(userAccelerometerEvents!.listen((UserAccelerometerEvent event) {
setState(() {
_userAccelerometerValues = <double>[event.x, event.y, event.z];
});
}));
_streamSubscriptions.add(proximityEvents!.listen((ProximityEvent event) {
setState(() {
_proximityValues = event.getValue();
});
}));
}
}