sfit_step_counter 0.0.13 copy "sfit_step_counter: ^0.0.13" to clipboard
sfit_step_counter: ^0.0.13 copied to clipboard

Step Counter is a lightweight Flutter package that detects steps in real-time using the accelerometer via sensors_plus.

๐Ÿƒโ€โ™‚๏ธ Step Counter #

A lightweight, reliable Flutter step counting utility that uses native Android step detection (via EventChannel) and accelerometer-based fallback enhanced with Kalman filtering and pedestrian status detection. Includes real-time updates for steps, walking speed, and calories burned.


โœ… Features #

  • โœ… Native Android step detection
  • โœ… Accelerometer fallback logic with filtering
  • โœ… Real-time step stream
  • โœ… Walking status: walking / stopped
  • โœ… Calories burned estimation
  • โœ… Walking speed calculation (km/h)
  • โœ… Persistent step tracking with SharedPreferences
  • โœ… Background execution with flutter_background

๐Ÿš€ Getting Started #

1๏ธโƒฃ Add Dependencies #

In your pubspec.yaml:

dependencies:
  sensors_plus: ^3.0.3
  shared_preferences: ^2.2.2
  flutter_background: ^1.0.0
  permission_handler: ^11.3.0

2๏ธโƒฃ Android Setup #

a. AndroidManifest.xml

Add the following outside the <application> tag:

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Inside the <application> tag:

<application
   ...>
   <service android:name="com.pravera.flutter_background.FlutterBackgroundService"
            android:enabled="true" android:exported="false" />
   <receiver android:enabled="true" android:exported="true"
             android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
     <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED"/>
       <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
     </intent-filter>
   </receiver>
</application>

b. android/app/build.gradle

Ensure minimum SDK version is 21 or higher:

defaultConfig {
  minSdkVersion 21
  ...
}

3๏ธโƒฃ Request Permissions #

Add the following before starting the counter:

import 'package:permission_handler/permission_handler.dart';

Future<void> requestPermissions() async {
  final status = await Permission.activityRecognition.request();
  if (!status.isGranted) {
    throw Exception("Activity Recognition permission denied");
  }
}

๐Ÿงฐ How to Use #

โœ… Step 1: Import and Initialize #

import 'package:your_project/step_counter.dart';

final stepCounter = StepCounter();

await requestPermissions();
await stepCounter.init(weightKg: 70, heightMeters: 1.75);

โœ… Step 2: Start the Counter #

await stepCounter.start();

โœ… Step 3: Listen to Updates #

stepCounter.stepStream.listen((steps) {
  print('Steps: $steps');
  print('Calories: ${stepCounter.caloriesBurned.toStringAsFixed(2)} kcal');
  print('Speed: ${stepCounter.walkingSpeedKmh.toStringAsFixed(2)} km/h');
});

โœ… Step 4: Stop or Reset #

await stepCounter.stop();
await stepCounter.reset();

๐Ÿ“ฆ StepData DTO (Optional) #

If you choose to stream richer objects instead of just step counts:

class StepData {
  final int steps;
  final String status; // "walking" or "stopped"
  final double calories;
  final double speed; // in km/h
}

Update the stream to emit StepData if needed.


๐Ÿ’ก Tips #

  • ๐Ÿ“ฑ Keep the app running in background using flutter_background.
  • โœ… Ensure permissions are granted, or step tracking will not work.
  • ๐Ÿงช You can add unit tests using mock streams if needed.
  • ๐Ÿง  Use Googleโ€™s Activity Recognition API for more advanced use cases.

๐Ÿ› Troubleshooting #

Problem Solution
Steps not counting Ensure ACTIVITY_RECOGNITION permission is granted.
App stops counting in background Ensure flutter_background is properly configured and active.
Counter is too sensitive Adjust debounce threshold (e.g., time between steps or magnitude) in the logic.

โœ… Example Integration #

void main() async {
  final stepCounter = StepCounter();

  await requestPermissions();
  await stepCounter.init(weightKg: 70, heightMeters: 1.75);
  await stepCounter.start();

  stepCounter.stepStream.listen((steps) {
    print('Steps: $steps');
    print('Calories: ${stepCounter.caloriesBurned.toStringAsFixed(2)} kcal');
    print('Speed: ${stepCounter.walkingSpeedKmh.toStringAsFixed(2)} km/h');
  });
}

๐Ÿ”ฎ Coming Soon #

  • โŒ iOS support
  • โŒ Step goals
  • โŒ Integration with Google Fit / Apple Health
  • โŒ Weekly/monthly stats
  • โŒ Notification badge with daily steps

๐Ÿค Contributing #

Feel free to submit issues, improvements, or PRs! You can also ask for:

  • GetX integration (StepsController)
  • ML-based activity recognition
  • Chart display of progress
4
likes
0
points
77
downloads

Publisher

unverified uploader

Weekly Downloads

Step Counter is a lightweight Flutter package that detects steps in real-time using the accelerometer via sensors_plus.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, flutter_background, sensors_plus, shared_preferences

More

Packages that depend on sfit_step_counter