Search code examples
androidflutteraccelerometergyroscope

Flutter - Activity Tracker App - steps, upstairs/downstairs, stationary


I would like to use Flutter to create an app that tracks the user's current activity state - whether they are going up or down stairs, or if they are walking or stationary. The reason why I am using Flutter is because it needs to be integrated to an existing Flutter app, managed by another team.

I am new to software development. I would like to use the phone sensors: gyroscope, accelerometer and barometer. May I know where to start? E.g. useful packages and APIs, platform channels?


Solution

  • You can use package https://pub.dev/packages/sensors
    example code https://github.com/flutter/plugins/blob/master/packages/sensors/example/lib/main.dart

    code snippet

    @override
      Widget build(BuildContext context) {
        final List<String> accelerometer =
            _accelerometerValues?.map((double v) => v.toStringAsFixed(1))?.toList();
        final List<String> gyroscope =
            _gyroscopeValues?.map((double v) => v.toStringAsFixed(1))?.toList();
        final List<String> userAccelerometer = _userAccelerometerValues
            ?.map((double v) => v.toStringAsFixed(1))
            ?.toList();
    
    @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];
          });
        }));
      }