Have any questions:

Toll free:9801887718Available 24/7

Email our experts:info@mantraideas.com

In: Flutter, Hardware & Sensors

Flutter makes it incredibly easy to work with hardware sensors such as the accelerometer, gyroscope, magnetometer, compass, proximity sensor, light sensor, and GPS. With these Flutter sensors, your app can react to real-world movement, orientation, proximity, and environmental inputs — unlocking endless possibilities like navigation apps, fitness trackers, AR utilities, gesture-controlled games, and more.

In this complete tutorial, you’ll learn:

  • How each sensor works
  • What you can build with them
  • How to use sensors_plus Flutter, geolocator, flutter_compass, and other packages
  • How to combine all sensors into a real-time interactive demo
  • How to use motion, orientation, proximity, and light data in your app

If you’ve ever wondered “can you access sensors with Flutter?” — the answer is yes, and this guide shows exactly how.

1. Packages Required

Add the following to your pubspec.yaml:

dependencies:
  sensors_plus: ^7.0.0
  geolocator: ^14.0.2
  flutter_compass: ^0.8.1

Packages  roles:

  • sensors_plus Flutter / sensors plus flutter → accelerometer, gyroscope, magnetometer
  • geolocator → GPS, live location tracking
  • flutter_compass → compass heading

2. Accelerometer in Flutter

The accelerometer measures acceleration along X, Y, Z axes.

Common uses:

  • Shake detection
  • Gesture controls
  • Motion-based gameplay
  • Step counter logic

Detecting device tilt

Code: Listening to Accelerometer Events

_accelerometerSubscription = accelerometerEventStream().listen(
  (AccelerometerEvent event) {
    setState(() {
//including z axis involves acceleration due to gravity as well
      accelX = event.x;
      accelY = event.y;
      accelZ = event.z;

      double magnitude = sqrt(accelX * accelX + accelY * accelY + accelZ * accelZ,);
      isShaking = magnitude > 20;
    });
  },
);

This setup shows the basics of how to use sensors in Flutter and illustrates how a shake gesture is detected.

3. Gyroscope in Flutter

The gyroscope measures device rotation. Perfect for:

  • AR features
  • Motion-controlled UI
  • Advanced gesture detection

Orientation-based navigation

Gyroscope Listener

_gyroscopeSubscription = gyroscopeEventStream().listen(
  (GyroscopeEvent event) {
    setState(() {
      gyroX = event.x;
      gyroY = event.y;
      gyroZ = event.z;
    });
  },
);

4. Magnetometer in Flutter

The magnetometer detects magnetic field variations — the foundation for compass calculations.

Use cases:

  • Metal detector apps
  • Improving heading accuracy
  • Environmental/industrial tools

Magnetometer Listener

_magnetometerSubscription = magnetometerEventStream().listen(
  (MagnetometerEvent event) {
    setState(() {
      magX = event.x;
      magY = event.y;
      magZ = event.z;
    });
  },
);

5. Compass Heading with flutter_compass

The compass uses magnetometer + accelerometer to compute device heading.

Compass Listener

_compassSubscription = FlutterCompass.events?.listen(
  (CompassEvent event) {
    setState(() {
      compassAvailable = true;
      heading = event.heading ?? 0;
      direction = _getDirection(heading);
    });
  },
  onError: (error) {
    compassAvailable = false;
  },
);

Convert Heading to Direction Text

String _getDirection(double heading) {
  if (heading >= 337.5 || heading < 22.5) return 'N ⬆️';
  if (heading >= 22.5 && heading < 67.5) return 'NE ↗️';
  if (heading >= 67.5 && heading < 112.5) return 'E ➡️';
  if (heading >= 112.5 && heading < 157.5) return 'SE ↘️';
  if (heading >= 157.5 && heading < 202.5) return 'S ⬇️';
  if (heading >= 202.5 && heading < 247.5) return 'SW ↙️';
  if (heading >= 247.5 && heading < 292.5) return 'W ⬅️';
  return 'NW ↖️';
}

This is one of the most common use cases for a Flutter sensor Android or iOS implementation.

6. GPS & Location Tracking (Geolocator)

You can read:

  • Latitude
  • Longitude
  • Speed
  • Altitude
  • Accuracy
  • Live movement

6.1 Check Location Permission

Future<void> _checkLocationPermission() async {
  LocationPermission permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
  }

  locationPermissionGranted =
      permission == LocationPermission.always ||
      permission == LocationPermission.whileInUse;
}

6.2 Get Current Location

Position position = await Geolocator.getCurrentPosition(
  desiredAccuracy: LocationAccuracy.high,
);

setState(() {
  latitude = position.latitude;
  longitude = position.longitude;
  altitude = position.altitude;
  speed = position.speed;
  accuracy = position.accuracy;
});

6.3 Real-Time Location Stream

_locationSubscription = Geolocator.getPositionStream(
  locationSettings: const LocationSettings(
    accuracy: LocationAccuracy.high,
    distanceFilter: 10,
  ),
).listen((Position position) {
  setState(() {
    latitude = position.latitude;
    longitude = position.longitude;
    altitude = position.altitude;
    speed = position.speed;
    accuracy = position.accuracy;
  });
});

7. UI for Sensor Cards

You built a clean modular card widget:

Widget _buildSensorCard(
  String title,
  IconData icon,
  Color color,
  List<String> data, {
  String? subtitle,
  Widget? action,
})

8. Full Combined Sensors Demo

Your full screen displays:

  • Accelerometer (shake detection)
  • Gyroscope
  • Magnetometer
  • Compass heading
  • GPS continuous tracking

Reference Project here
https://github.com/Rulson/flutter_sensors

9. Best Practices

Cancel Streams

@override
void dispose() {
  _accelerometerSubscription?.cancel();
  _gyroscopeSubscription?.cancel();
  _magnetometerSubscription?.cancel();
  _compassSubscription?.cancel();
  _locationSubscription?.cancel();
  super.dispose();
}

Additional Tips

  • Limit UI rebuilds — sensors emit 100+ updates/sec 
  • Handle missing sensors (e.g., no compass on some phones)
  • Request permissions properly
  • Test on real hardware (not emulators)

Conclusion

Flutter provides an incredibly powerful and simple way to integrate hardware sensors into your mobile apps. Using Packages like sensors_plus Flutter, sensor plus flutter, flutter sensor plus, geolocator, flutter_compass, and optional flutter proximity sensor or flutter light sensor, you can build real-time interactive apps that react to the physical world.

By combining accelerometer, gyroscope, magnetometer, compass, GPS, light sensor, and proximity sensor data, you now have a complete Flutter sensor package dashboard ready to adapt into your next project.If you’re exploring Flutter sensor Android/iOS development, this guide gives you everything you need to start building powerful sensor-driven applications.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *