Flutter accelerometer/gyroscope sensor lag

Viewed 1730

I've been trying to implement a gyroscope image viewer using the sensors package, however, the result seems to be very laggy. I have found a similar project on YouTube which is trying to achieve a similar goal, but as you can see in the video the animation is also very laggy.

The following code is simply outputting the data from the event, I notice how the data is being updated lags like 50ms in between updates.

Is there a way to smoothen the animation or update the data faster? Or is this a Flutter limitation?

NOTE: I have tried --release version as suggested by other posts but the result stays the same.

import 'package:sensors/sensors.dart';

class MyGyro extends StatefulWidget {
  final Widget child;

  MyGyro({this.child});

  @override
  _MyGyroState createState() => _MyGyroState();
}

class _MyGyroState extends State<MyGyro> {
  double gyroX = 0;
  double gyroY = 0;

  @override
  void initState() {
    super.initState();

    gyroscopeEvents.listen((GyroscopeEvent event) {
      setState(() {
        gyroX = ((event.x * 100).round() / 100).clamp(-1.0, 1.0) * -1;
        gyroY = ((event.y * 100).round() / 100).clamp(-1.0, 1.0);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 100,
      width: 100,
      child: Transform.translate(
        offset: Offset(gyroY, 0),
        child: Container(
          child: Center(
            child: Column(
              children: [Text("X: ${gyroX}"), Text("Y: ${gyroY}"),],
            ),
          ),
        ),
      ),
    );
  }
}
1 Answers

I have found that is purely the problem of the sensors package I was using, either they have hard coded a slower interval when listening to the sensor event, or they are just using the default interval by the IOS channel.

So, I have found another package called flutter_sensors which had solved the problem. It's a very simple API to access the sensor events, but it allows you to change the interval.

Related