workout 0.6.2 workout: ^0.6.2 copied to clipboard
Run a workout session on Wear OS and Tizen. Get data such as heart rate in real time.
import 'package:flutter/material.dart';
import 'package:wear/wear.dart';
import 'package:workout/workout.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final workout = Workout();
final exerciseType = ExerciseType.walking;
final features = [
WorkoutFeature.heartRate,
WorkoutFeature.calories,
WorkoutFeature.steps,
WorkoutFeature.distance,
WorkoutFeature.speed,
];
final enableGps = true;
double heartRate = 0;
double calories = 0;
double steps = 0;
double distance = 0;
double speed = 0;
bool started = false;
_MyAppState() {
workout.stream.listen((event) {
// ignore: avoid_print
print('${event.feature}: ${event.value} (${event.timestamp})');
switch (event.feature) {
case WorkoutFeature.unknown:
return;
case WorkoutFeature.heartRate:
setState(() {
heartRate = event.value;
});
break;
case WorkoutFeature.calories:
setState(() {
calories = event.value;
});
break;
case WorkoutFeature.steps:
setState(() {
steps = event.value;
});
break;
case WorkoutFeature.distance:
setState(() {
distance = event.value;
});
break;
case WorkoutFeature.speed:
setState(() {
speed = event.value;
});
break;
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: Colors.black),
// Use ambient mode to stay alive in the foreground
// Use a foreground service if you want to stay alive in the background
home: AmbientMode(
builder: (context, mode, child) => child!,
child: Scaffold(
body: Center(
child: Column(
children: [
const Spacer(),
Text('Heart rate: $heartRate'),
Text('Calories: ${calories.toStringAsFixed(2)}'),
Text('Steps: $steps'),
Text('Distance: ${distance.toStringAsFixed(2)}'),
Text('Speed: ${speed.toStringAsFixed(2)}'),
const Spacer(),
TextButton(
onPressed: toggleExerciseState,
child: Text(started ? 'Stop' : 'Start'),
),
],
),
),
),
),
);
}
void toggleExerciseState() async {
setState(() {
started = !started;
});
if (started) {
final supportedExerciseTypes = await workout.getSupportedExerciseTypes();
// ignore: avoid_print
print('Supported exercise types: ${supportedExerciseTypes.length}');
final result = await workout.start(
// In a real application, check the supported exercise types first
exerciseType: exerciseType,
features: features,
enableGps: enableGps,
);
if (result.unsupportedFeatures.isNotEmpty) {
// ignore: avoid_print
print('Unsupported features: ${result.unsupportedFeatures}');
// In a real application, update the UI to match
} else {
// ignore: avoid_print
print('All requested features supported');
}
} else {
await workout.stop();
}
}
}