sensors_plus 2.0.5 sensors_plus: ^2.0.5 copied to clipboard
Flutter plugin for accessing accelerometer, gyroscope, and magnetometer sensors.
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// ignore_for_file: public_member_api_docs
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:sensors_plus/sensors_plus.dart';
import 'snake.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sensors Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static const int _snakeRows = 20;
static const int _snakeColumns = 20;
static const double _snakeCellSize = 10.0;
List<double>? _userAccelerometerValues;
List<double>? _accelerometerValues;
List<double>? _gyroscopeValues;
List<double>? _magnetometerValues;
final _streamSubscriptions = <StreamSubscription<dynamic>>[];
@override
Widget build(BuildContext context) {
final userAccelerometer = _userAccelerometerValues
?.map((double v) => v.toStringAsFixed(1))
.toList();
final accelerometer =
_accelerometerValues?.map((double v) => v.toStringAsFixed(1)).toList();
final gyroscope =
_gyroscopeValues?.map((double v) => v.toStringAsFixed(1)).toList();
final magnetometer =
_magnetometerValues?.map((double v) => v.toStringAsFixed(1)).toList();
return Scaffold(
appBar: AppBar(
title: const Text('Sensor Example'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Center(
child: DecoratedBox(
decoration: BoxDecoration(
border: Border.all(width: 1.0, color: Colors.black38),
),
child: SizedBox(
height: _snakeRows * _snakeCellSize,
width: _snakeColumns * _snakeCellSize,
child: Snake(
rows: _snakeRows,
columns: _snakeColumns,
cellSize: _snakeCellSize,
),
),
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('UserAccelerometer: $userAccelerometer'),
],
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Accelerometer: $accelerometer'),
],
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Gyroscope: $gyroscope'),
],
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Magnetometer: $magnetometer'),
],
),
),
],
),
);
}
@override
void dispose() {
super.dispose();
for (final subscription in _streamSubscriptions) {
subscription.cancel();
}
}
@override
void initState() {
super.initState();
_streamSubscriptions.add(
userAccelerometerEvents.listen(
(UserAccelerometerEvent event) {
setState(() {
_userAccelerometerValues = <double>[event.x, event.y, event.z];
});
},
),
);
_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(
magnetometerEvents.listen(
(MagnetometerEvent event) {
setState(() {
_magnetometerValues = <double>[event.x, event.y, event.z];
});
},
),
);
}
}