diff --git a/content/articles/How-to-use-the-gyroscope-through-a-flutter-app/Images/hierarchy.png b/content/articles/How-to-use-the-gyroscope-through-a-flutter-app/Images/hierarchy.png new file mode 100644 index 00000000000..3f6bef37dd3 Binary files /dev/null and b/content/articles/How-to-use-the-gyroscope-through-a-flutter-app/Images/hierarchy.png differ diff --git a/content/articles/How-to-use-the-gyroscope-through-a-flutter-app/gyromark.md b/content/articles/How-to-use-the-gyroscope-through-a-flutter-app/gyromark.md new file mode 100644 index 00000000000..4b035bc6ab8 --- /dev/null +++ b/content/articles/How-to-use-the-gyroscope-through-a-flutter-app/gyromark.md @@ -0,0 +1,497 @@ +### Overview + +Motion is a simple concept that has escaped most technological discussions. Have you ever wondered how your gadget determines whether or not it is in motion or what direction it is in? This is all done via sensors that are built into your device, and the objective of this tutorial is to show you how I retrieved data from these sensors using a flutter app. + +### Table of Content + + - [Overview](#overview) + - [Prerequisites](#prerequisites) + - [Flutter sensor library introduction](#flutter-sensor-library-introduction) + - [AccelerometerEvent](#accelerometerevent) + - [UserAccelerometer Event sensor](#useraccelerometer-event-sensor) + - [GyroscopeEvent.](#gyroscopeevent) + - [Flutter app](#flutter-app) + - [Hierarchy](#hierarchy) + - [Create a new flutter project](#create-a-new-flutter-project) + - [Add the sensor dependencies](#add-the-sensor-dependencies) + - [import the package](#import-the-package) + - [snippet code example](#snippet-code-example) + - [app layout and navigation](#app-layout-and-navigation) + - [Using the accerolemeter sensor](#using-the-accerolemeter-sensor) + - [The full example of the accelerometerEvent sensor in an app](#the-full-example-of-the-accelerometerevent-sensor-in-an-app) + - [Using the gyroscope](#using-the-gyroscope) + - [the full example of the gyroscopeEvent sensor in an app](#the-full-example-of-the-gyroscopeevent-sensor-in-an-app) + - [Conclusion](#conclusion) + - [Further reading](#further-reading) + + +### Prerequisites +To follow along with this tutorial, you’ll need the following: + +* A basic understanding of the object oriented programming language. +* Have flutter installed in your system. +* If you do not have flutter installed in yours system click [here](https://docs.flutter.dev/get-started/install) to install +* A foundation in flutter app development is required. + +### Flutter sensor library introduction +To detect the motion and orientation of your device, flutter has a sensor library that allows you to do so. +which include: + + * UserAccelerometerEvent sensor + * AccelerometerEvent + * GyroscopeEvent + + + ### AccelerometerEvent + It is used to measure the velocity of an object at rest i.e proper acceleration. Imaine your device in your hand it may seem motion less but there is gravity acting on it hence it tends to move down wards to the surface at a constant downward force of g ≈ 9.81 m/s2.Flutter Accelerometer simply uses accelerometer reading and tell wheather the device is currently in motion and if so it's direction .To get more on the accelerometer[click here](https://en.wikipedia.org/wiki/Accelerometer) + + ### UserAccelerometer Event sensor + This works in the same way as the accelerometerEvent but in this case gravity is ignored and uses the user motion instead. + + #### GyroscopeEvent. + The gyroscope sensor is also known as angular velocity sensors, are devices that sense angular velocity. In simple terms, angular velocity is the change in rotational angle per unit of time. Angular velocity is generally expressed in deg/s (degrees per second).The data from the sensor is used to determine the orientation of the device. + For more information on gyroscope [click here](https://en.wikipedia.org/wiki/Gyroscope) + + +### Flutter app + Now that we have understood what we are about to implement lets dive right into it. +### Hierarchy + For easy while coding kindly follow the file arrangement above +![file arrangement](Images/hierarchy.png) + + ### Create a new flutter project + First you create a new flutter project by running the code below on your terminal + ```flutter + flutter create gyro + ``` + or open an existing project where you what to implement this but for quick and easy understanding we are going to a new project. + + ### Add the sensor dependencies + After successfully creating a flutter project you need to add the dependencies that allow you to access the sensors_plus package. + do this by adding the following lines in pubspec.yaml file. + Get the latest version of Flutter Sensor package here from official site [here](https://pub.dev/packages/sensors_plus/changelog) + + ```flutter + dependencies: + flutter: + sdk: flutter + sensors_plus: ^1.2.2 + ``` + + ### import the package + In the main dart file you need to import the package by writting the following code. + ``` + import 'package:sensors_plus/sensors_plus.dart'; + + ``` + This line of code can also be added to any to any file you want to implement it like we are goin to do in our examples later on in ths tutorial. + + ### snippet code example + + The sensors are accessed through streams with the following snippet codes. + ```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)] + ``` +### app layout and navigation + In my case i am using visual code to write the code. + To create a well designed app,a nice and simple layout is key,hence we are goin to start with creating the apps layout + first you need to clear all the contents in the main.dart file.After that you need to coppy the following code there. + ```dart +import 'package:flutter/material.dart'; +import './ accelerometer.dart'; +import './gyroscope.dart'; + +Future main() async { + runApp(MyApp()); +} + + +class MyApp extends StatelessWidget{ + @override + Widget build(BuildContext context) { + return MaterialApp( + home: Nav(), + ); + } +} + + + +class Nav extends StatefulWidget { + const Nav({ Key? key }) : super(key: key); + + @override + State