title: How to detect device motion direction in Flutter?
body: <p>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.</p>
  <p>Here is what I've tried:</p>
  <p>I have tried using <a href="https://pub.dev/packages/sensors_plus" rel="nofollow noreferrer">sensors_plus</a> library for this and tried using the accelerometer for detecting left or right direction and the program is given below.</p>
  <pre><code>void initState() {
  userAccelerometerEvents.listen((UserAccelerometerEvent event) {
    var me = event.x;
  
    var me2 = num.parse(me.toStringAsFixed(2));
    print(me2);
  
    if (me2.round() &lt; 0) {
      setState(() {
        direction = &quot;Left&quot;;
      });
    }
    if (me2 &gt; 0) {
      if (me2 &gt; 0.5) {
        setState(() {
          direction = &quot;Right&quot;;
        });
      } else {
        setState(() {
          direction = &quot;Still&quot;;
        });
      }
    }
  });
  super.initState();}
  </code></pre>
  <p>the outputs I get are:</p>
  <p><a href="https://i.stack.imgur.com/IN6ZN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IN6ZN.png" alt="enter image description here" /></a></p>
  <p>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 <code>&quot;event.x&quot;</code> but in that case, the output switches between &quot;Left&quot; and &quot;Right&quot; and &quot;Still&quot; really quickly even when the device is still.</p>
  <p>I could not find any example that covers this problem online or in the package docs or examples.</p>
  <p>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.</p>
  <p>Should I use another sensor?</p>
  <p>I do not fully understand the magnetometer's inputs which I've also tried using. Any help in understanding that is appreciated.</p>
  <p>I have also tried searching for a package on pub.dev but I can't find any that fit this need.</p>
  <p>Any help is appreciated thanks a lot in advance.</p>