sensors 0.4.2+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 98

sensors #

Please set your constraint to sensors: '>=0.4.y+x <2.0.0'

Backward compatible 1.0.0 version is coming #

The sensors plugin has reached a stable API, we guarantee that version 1.0.0 will be backward compatible with 0.4.y+z. Please use sensors: '>=0.4.y+x <2.0.0' as your dependency constraint to allow a smoother ecosystem migration. For more details see: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0

A Flutter plugin to access the accelerometer and gyroscope sensors.

Usage #

To use this plugin, add sensors as a dependency in your pubspec.yaml file.

This will expose three classes of sensor events, through three different streams.

  • AccelerometerEvents describe the velocity of the device, including the effects of gravity. Put simply, you can use accelerometer readings to tell if the device is moving in a particular direction.
  • UserAccelerometerEvents also describe the velocity of the device, but don't include gravity. They can also be thought of as just the user's affect on the device.
  • GyroscopeEvents describe the rotation of the device.

Each of these is exposed through a BroadcastStream: accelerometerEvents, userAccelerometerEvents, and gyroscopeEvents, respectively.

Example #

import 'package:sensors/sensors.dart';

accelerometerEvents.listen((AccelerometerEvent event) {
  print(event);
});
// [AccelerometerEvent (x: 0.0, y: 9.8, z: 0.0)]

userAccelerometerEvents.listen((UserAccelerometerEvent event) {
  print(event);
});
// [UserAccelerometerEvent (x: 0.0, y: 0.0, z: 0.0)]

gyroscopeEvents.listen((GyroscopeEvent event) {
  print(event);
});
// [GyroscopeEvent (x: 0.0, y: 0.0, z: 0.0)]

Also see the example subdirectory for an example application that uses the sensor data.

0.4.2+2 #

  • Post-v2 Android embedding cleanup.

0.4.2+1 #

  • Update lower bound of dart dependency to 2.1.0.

0.4.2 #

  • Remove Android dependencies fallback.
  • Require Flutter SDK 1.12.13+hotfix.5 or greater.
  • Fix CocoaPods podspec lint warnings.

0.4.1+10 #

0.4.1+9 #

  • Replace deprecated getFlutterEngine call on Android.

0.4.1+8 #

  • Make the pedantic dev_dependency explicit.

0.4.1+7 #

  • Fixed example userAccelerometerEvent in documentation

0.4.1+6 #

  • Migrate from deprecated BinaryMessages to ServicesBinding.instance.defaultBinaryMessenger.
  • Require Flutter SDK 1.12.13+hotfix.5 or greater (current stable).

0.4.1+5 #

  • Fix example setState() called after dispose() by canceling the timer.

0.4.1+4 #

  • Remove the deprecated author: field from pubspec.yaml
  • Migrate the plugin to the pubspec platforms manifest.
  • Require Flutter SDK 1.10.0 or greater.

0.4.1+3 #

  • Improve documentation and add unit test coverage.

0.4.1+2 #

  • Remove AndroidX warnings.

0.4.1+1 #

  • Include lifecycle dependency as a compileOnly one on Android to resolve potential version conflicts with other transitive libraries.

0.4.1 #

  • Support the v2 Android embedder.
  • Update to AndroidX.
  • Migrate to using the new e2e test binding.
  • Add a e2e test.

0.4.0+3 #

  • Update and migrate iOS example project.
  • Define clang module for iOS.

0.4.0+2 #

0.4.0+1 #

  • Log a more detailed warning at build time about the previous AndroidX migration.

0.4.0 #

  • Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.

0.3.5 #

  • Added missing test package dependency.

0.3.4 #

  • Make sensors Dart 2 compliant.

0.3.3 #

  • Updated Gradle tooling to match Android Studio 3.1.2.

0.3.2 #

  • Added user acceleration sensor events (i.e. accelerometer without gravity).

0.3.1 #

  • Fixed Dart 2 type error with iOS sensor events.

0.3.0 #

  • Breaking change. Set SDK constraints to match the Flutter beta release.

0.2.1 #

  • Fixed warnings from the Dart 2.0 analyzer.
  • Simplified and upgraded Android project template to Android SDK 27.
  • Updated package description.

0.2.0 #

  • Breaking change. Upgraded to Gradle 4.1 and Android Studio Gradle plugin 3.0.1. Older Flutter projects need to upgrade their Gradle setup as well in order to use this version of the plugin. Instructions can be found here.

0.1.1 #

  • Added FLT prefix to iOS types.

0.1.0 #

  • Initial Open Source release.

example/lib/main.dart

// 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/sensors.dart';

import 'snake.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, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState 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> _accelerometerValues;
  List<double> _userAccelerometerValues;
  List<double> _gyroscopeValues;
  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>[
          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(
            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),
          ),
        ],
      ),
    );
  }

  @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];
      });
    }));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  sensors: ^0.4.2+2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:sensors/sensors.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
95
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
98
Learn more about scoring.

We analyzed this package on May 23, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev
  • Flutter: 1.17.0

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0<3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
e2e ^0.2.0
flutter_test
mockito ^4.1.1
pedantic ^1.8.0
test ^1.3.0