- 28 Sep 2023
- 3 Minutes to read
- Print
- DarkLight
Flutter - SMFit plugin
- Updated on 28 Sep 2023
- 3 Minutes to read
- Print
- DarkLight
This article is a step-by-step guide to how to integrate the smfit_fl_plugin into your Flutter application.
1. Installation
In order to install thesmfit_fl_plugin package run this code in the application terminal
flutter pub add smfit_fl_plugin
This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):
dependencies:
smfit_fl_plugin: ^0.1.0+3
In your Podfile (should be in app_name/ios) add the sources in the file header
source 'https://github.com/CocoaPods/Specs.git'
source 'https://bitbucket.org/sency-ios/sency_ios_sdk.git'
also add these install hooks
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '14.5'
end
end
end
2. Configuration
To work with the sdk you should configure the package (function will throw an error if we skip this stage).
In your first app screen import the package:
import 'package:smfit_fl_plugin/smfit_fl_plugin.dart';
Thenadd the following code in State class:
class _MyAppState extends State<MyApp> {
bool _didSuccess = false;
final _smfitFlPlugin = SMFitFlPlugin();
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
bool didSuccess;
// Before doing anything you need to call configSMFit with your key and wait for the result if not you will not be able to access plugin functions
var result = await _smfitFlPlugin.configSMFit("YOUR_KEY");
didSuccess = result.$1;
if (!mounted) return;
setState(() {
_didSuccess = didSuccess;
});
}
}
.
.
.
3. Implement SMFit callbacks
Implement SMFitFlPluginDelegate
class _MyAppState extends State<MyApp> implements SMFitFlPluginDelegate
and the add this function
@override
void captureSessionStop() {
// This function will be called when the session stopped and the camera is no longer available
}
@override
void didReceiveError(String? error) {
// This function will be called every time tou will get a error
print(error);
}
@override
void didReceiveMovementData(MovementData movementData) {
// This function will be called every frame
// movementData - This struct holds all the feedback data:
// didDetectRep - (Dynamic exercise) If user did perform repetition
// isGoodRep - (Dynamic exercise) true if the repetition was good
// isInPosition - (Static/Isometric exercise type) true if the user in position
// currentRomValue - The current Range Of Motion of the user
// specialParams - some dynamic exercises will have some special params for example the exercise "Jumps" has "JumpPeakHeight" and "currHeight"
print(movementData);
}
@override
void didReceivePoseData(List<PoseData> poseData) {
//This function will be called every frame
//poseData - This a array of joints and there position (can be empty if the user was not detected in the camera field of view)
print(poseData);
}
@override
void captureSessionStart(){
//Once the camera data is available this function will be called
}
API
Use SMFit methods to start or end a session, and start or end detection.
void startSession() async {
// Call this function to start the session
String? error = await _smfitFlPlugin.startSession();
print("Start Detection error: ${error ?? "non"}");
}
void startDetection() async {
// Call startDetection to start a exercise
// exercise options: AlternateWindmillToeTouch ,Burpees ,Crunches ,GlutesBridge ,HighKnees ,JumpingJacks ,LateralRaises ,Lunge ,LungeFront ,LungeSide ,LungeSideLeft ,LungeSideRight ,LungeSideSplit ,PlankHighKneeToElbow ,PlankHighShoulderTaps ,PushupRegular ,Jumps ,ReverseSitToTableTop ,ShouldersPress ,SideStepJacks ,SkaterHops ,SkiJumps ,SquatAndRotationJab ,SquatRegular ,StandingObliqueCrunches ,StandingStepReverseAirFly ,SumoSquatSpreadHandsVariation ,LungeRegularStatic ,PlankHighStatic ,SquatRegularOverheadStatic ,SquatRegularStatic ,SquatSumoStatic ,TuckHold ,StandingSideBendRight ,StandingSideBendLeft ,JeffersonCurlRight ,LungeSideStaticRight ,LungeSideStaticLeft, SquatRegularOverhead
String? error = await _smfitFlPlugin.startDetection("HighKnees");
print("Start Detection error: ${error ?? "non"}");
}
void stopDetection() async{
var result = await _smfitFlPlugin.stopDetection();
String? error = result.$1;
Map<String, Object?> data = result.$2;
print("Stop Detection error: ${error ?? "non"} data: $data");
}
void stopSession() async {
var result = await _smfitFlPlugin.stopSession();
String? error = result.$1;
Map<String, Object?> data = result.$2;
print("Stop Session error: ${error ?? "non"} data: $data");
}
- startSession - this function will start the session
- startDetection - this function will stat the exercise detection
- Go to Sency's exercise library to view the currently supported exercise types
5. Adding the Camera widget
Optionally you can add the CameraWidget simply call it after the session starts to see the camera for exemple
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('SMFit example app'),
),
body: Center(
child: Column(
children: [
Text('Auth accepted: $_didSuccess\n'),
SizedBox(
height: 500,
child: CameraWidget(),
),
TextButton(onPressed: startSession, child: const Text("Start Session")),
TextButton(onPressed: startDetection, child: const Text("Start Detection")),
TextButton(onPressed: stopDetection, child: const Text("Stop Detection")),
TextButton(onPressed: stopSession, child: const Text("Stop Session"))
],
)
),
),
);
}