Code Setup: Only WorkoutSetScreen
Add the required packages for GOFA Pose SDK
Our GOFA SDK uses GetIt to register our GofaPoseSdkManager as a singleton service helps with initialization of our SDK functionalities.
So please run the following command to add the dependencies
flutter pub add get_it
flutter pub add provider
flutter pub add bloc
flutter pub add flutter_bloc
Starting WorkoutSetScreen directly
ElevatedButton(
onPressed: () async {
await registerGofaPoseSdk(
context: context,
);
if (context.mounted) {
await navigateToWorkoutSetScreen(
context: context,
workoutItem: PoseItem(
key: 'squats',
localizedNames: {
en: "Squats",
zh_Hant: "深蹲",
zh_Hans: "深蹲",
},
),
totalSecondsAllowed: totalSecondsAllowed,
targetRepsToStart: targetRepsToStart,
autoStopSetWhenRepsFulfilled: false,
);
}
},
child: const Text('Start Workout'),
),
Future<void> registerGofaPoseSdk({
required BuildContext context,
}) async {
// final isPhysicalDevice = await checkPhysicalDevice();
if (!GetIt.I.isRegistered<GofaPoseSdkManager>()) {
final gofaPoseSDKManager = GofaPoseSdkManager();
GetIt.I.registerSingleton<GofaPoseSdkManager>(gofaPoseSDKManager);
}
final gofaPoseSDKManager = GetIt.I<GofaPoseSdkManager>();
try {
if (gofaPoseSDKManager.isInitialized) {
log.d('GofaPoseSdkManager is already initialized');
return;
}
// call gofaPoseSDKManager.init
gofaPoseSDKManager
.init(); // do not await so that splash screen can be shown
} catch (e) {
log.e('Error registering GofaPoseSdkManager: $e');
// show scaffold messenger
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Error occurred when starting GofaPoseSdkManager'),
),
);
}
}
}
Future<void> navigateToWorkoutSetScreen({
required BuildContext context,
required PoseItem workoutItem,
required int totalSecondsAllowed,
required int targetRepsToStart,
required bool autoStopSetWhenRepsFulfilled,
}) async {
if (context.mounted) {
try {
await GetIt.I<GofaPoseSdkManager>()
.isInitialized$
// timeout after 10 seconds, in case initialization fails
.timeout(const Duration(seconds: 10))
.contains(true);
} on TimeoutException catch (e) {
log.e('Error initializing GofaPoseSdkManager: $e');
if (context.mounted) {
// show scaffold with error message
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'$e',
),
),
);
}
}
GetIt.I<WorkoutSetBloc>().add(
WorkoutSetSetup(
workoutItem: workoutItem,
totalSecondsAllowed: totalSecondsAllowed,
targetRepsToStart: targetRepsToStart,
autoStopSetWhenRepsFulfilled: autoStopSetWhenRepsFulfilled,
isShowCheckpoint: true,
navigateToNextAfterWorkout: (_) {
// pop one screen
Navigator.of(context).pop();
},
),
);
if (!context.mounted) return;
// navigate to WorkoutSetScreen
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const WorkoutSetScreen(),
),
);
}
}
Example to get back the result of the last workout
// Show a button "Get last workout result"
ElevatedButton(
onPressed: () async {
await registerGofaPoseSdk(
context: context,
);
// wait till GofaPoseSdkManager is initialized
await GetIt.I<GofaPoseSdkManager>()
.isInitialized$
.timeout(const Duration(seconds: 5))
.contains(true);
final lastWorkoutSetState = GetIt.I<WorkoutSetBloc>().state;
log.d('lastWorkoutSetState: $lastWorkoutSetState');
if (context.mounted) {
// prompt a widget to show the last workout result
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Last Workout Result'),
content: Text(
'Workout Item: ${lastWorkoutSetState.workoutItem?.poseName}\n'
'Reps done: ${lastWorkoutSetState.repsDone}\n'
'Seconds Used: ${lastWorkoutSetState.secondsUsed}\n'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Close'),
),
],
);
},
);
}
},
child: const Text('Get last workout result'),
),
Add Gofa Pose SDK language support
Since version v2.0.0, the locales of our supported languages are:
Locale('en'),
Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant'),
Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'),
You are required to add GofaLocalizationsDelegate() to the localizationsDelegates array list of your MaterialApp.
MaterialApp(
...
localizationsDelegates: const [
AppLocalizations.delegate,
...
GofaLocalizationsDelegate(),
],
locale: currentLocale,
supportedLocales: yourSupportedLocales,
// supportedLocales: GofaLocalizations.supportedLocales,
...
)