sfit_step_counter 0.0.13
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