I am trying to detect the direction in which a device is moved. If a device (mobile or tablet) is picked up and moved to the right or left, I want to detect that using Flutter.
Here is what I've tried:
The program only shows left or right if I move the device really fast, although I would like to detect the direction on slower speeds. For this, I have tried to take smaller values of "event.x"
but in that case, the output switches between "Left" and "Right" and "Still" really quickly even when the device is still.
I could not find any example that covers this problem online or in the package docs or examples.
I would like to know how I can achieve this? I would like to know the left and right movement of the device with slow speeds too.
Should I use another sensor?
I do not fully understand the magnetometer's inputs which I've also tried using. Any help in understanding that is appreciated.
I have also tried searching for a package on pub.dev but I can't find any that fit this need.
Any help is appreciated thanks a lot in advance.
CodePudding user response:
There is a package called sensors_plus
will be helpful
find the video
video from flutter official youtube
CodePudding user response:
The plugin tells you about acceleration. If you want to detect motion, i.e. speed, you need to apply in integration filter to the data from those events. You may want to use UserAccellerationEvent
s since those already eliminate gravity.
Also, using events from the gyroscope will make you calculation more robust.
This is an application of sensor fusion by the way.
CodePudding user response:
You can try using location package to access device current location. It has attribute of heading which can be useful for you. You can use a listener to capture device location changes. Whenever device heading is changed, the heading value will be printed.
Location location = Location();
location.onLocationChanged.listen((LocationData currentLocation) {
// Use current location
print(currentLocation.heading);
});
Read more here: https://pub.dev/packages/location
Heading is a double value, that varies from 0 to 360 degrees. This image will help you to see what heading value means.