Flutter – Using Gyroscope Sensor
Last Updated :
19 Nov, 2023
Using a gyroscope sensor in a Flutter application can be helpful for various purposes, such as creating games, virtual reality experiences, or any application that relies on detecting the device’s orientation. In Flutter we can easily use the Gyroscope sensor using the sensors_plus package. In this article, we are going to fetch the data from the Gyroscope with the help of the sensors_plus package. A sample video is given below to get an idea about what we are going to do in this article.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Adding the Dependencies
Here we have to add the the following dependencies to our pubspec.yaml file.
dependencies:
sensors_plus: ^3.1.0
or simply you can run this command in your terminal .
flutter pub add sensors_plus
Step 3: Import the Package
First of all import material.dart package and the sensors_plus.dart package.
import 'package:flutter/material.dart';
import 'package:sensors_plus/sensors_plus.dart'; // Import the sensors package
Step 4: Execute the main Method
Here the execution of our app starts.
Dart
void main() => runApp(MyApp());
|
Step 5: Create MyApp Class
In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.
Dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false ,
home: GyroscopeExample(),
);
}
}
|
Step 6: Create GyroscopeExample Class
In this class we are going to use the Gyroscope to know the device position in X,Y,Z coordinates by using sensors_plus package.Here we create a Flutter application that listens to gyroscope sensor events and updates the displayed data as the device is rotated.
Dart
class GyroscopeExample extends StatefulWidget {
@override
_GyroscopeExampleState createState() => _GyroscopeExampleState();
}
class _GyroscopeExampleState extends State<GyroscopeExample> {
double _gyroX = 0.0;
double _gyroY = 0.0;
double _gyroZ = 0.0;
@override
void initState() {
super.initState();
gyroscopeEvents.listen((GyroscopeEvent event) {
setState(() {
_gyroX = event.x;
_gyroY = event.y;
_gyroZ = event.z;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( 'Gyroscope Example' ),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text( 'Gyroscope Data:' ),
Text( 'X: $_gyroX' ),
Text( 'Y: $_gyroY' ),
Text( 'Z: $_gyroZ' ),
],
),
),
);
}
}
|
Here is the full Code of main.dart file
Dart
import 'package:flutter/material.dart' ;
import 'package:sensors_plus/sensors_plus.dart' ;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false ,
home: GyroscopeExample(),
);
}
}
class GyroscopeExample extends StatefulWidget {
@override
_GyroscopeExampleState createState() => _GyroscopeExampleState();
}
class _GyroscopeExampleState extends State<GyroscopeExample> {
double _gyroX = 0.0;
double _gyroY = 0.0;
double _gyroZ = 0.0;
@override
void initState() {
super.initState();
gyroscopeEvents.listen((GyroscopeEvent event) {
setState(() {
_gyroX = event.x;
_gyroY = event.y;
_gyroZ = event.z;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( 'Gyroscope Example' ),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text( 'Gyroscope Data:' ),
Text( 'X: $_gyroX' ),
Text( 'Y: $_gyroY' ),
Text( 'Z: $_gyroZ' ),
],
),
),
);
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...