Getting device orientation (yaw, roll and pitch) in Flutter

5,816

Don't know if this is still relevant, as the question wasn't closed, but for those seeking the answer... there is now a Flutter Sensors package which does all the magic for you.

You'll have to subscribe to the streams to get the current values of the accelerometers and gyroscopes, of course.

For example:

  @override
  void initState() {
    super.initState();
    _streamSubscriptions
        .add(accelerometerEvents.listen((AccelerometerEvent event) {
      setState(() {
        _accelerometerValues = <double>[event.x, event.y, event.z];
      });
    }));
    _streamSubscriptions.add(gyroscopeEvents.listen((GyroscopeEvent event) {
      setState(() {
        _gyroscopeValues = <double>[event.x, event.y, event.z];
      });
    }));
    _streamSubscriptions
        .add(userAccelerometerEvents.listen((UserAccelerometerEvent event) {
      setState(() {
        _userAccelerometerValues = <double>[event.x, event.y, event.z];
      });
    }));
  }
Share:
5,816
Admin
Author by

Admin

Updated on December 06, 2022

Comments

  • Admin
    Admin over 1 year

    In Android I can get the device yaw, roll and pitch using a GAME_ROTATION_VECTOR sensor.

    I need to do the same thing in Flutter, but I haven't been able to find anything but the sensors package, which only gives access to accelerometer & gyroscope sensors.

    What can I do? Do I need to calculate the orientation myself from the accelerometer and gyro?