added workout widget and workout provider to handle different workout phases and durations

main
Julian Gegner 2023-02-27 01:20:00 +01:00
parent 20f5c5e426
commit b4c0015847
5 changed files with 76 additions and 3 deletions

View File

@ -3,8 +3,10 @@ import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:smoke_cess_app/providers/timer_provider.dart';
import 'package:smoke_cess_app/providers/workout_provider.dart';
import 'package:smoke_cess_app/widgets/popup_for_start_and_stop.dart';
import 'package:smoke_cess_app/widgets/timer_widget.dart';
import 'package:smoke_cess_app/widgets/workout_timer_widget.dart';
import '../providers/input_provider.dart';
@ -168,7 +170,14 @@ class _IntervalTimerPageState extends State<IntervalTimerPage> {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => TimerProvider()),
ChangeNotifierProvider(create: (context) => WorkoutProvider())
],
child: WorkoutTimerWidget(),
);
ChangeNotifierProvider(
create: (context) => TimerProvider(),
child: TimerWidget(
duration: Duration(seconds: 5),

View File

@ -15,7 +15,7 @@ class MyHomePage extends StatefulWidget {
}
class MyHomePageState extends State<MyHomePage> {
int _selectedIndex = 4;
int _selectedIndex = 2;
int? _gruppe;
final List<String> _titles = [

View File

@ -9,9 +9,9 @@ class TimerProvider extends ChangeNotifier {
void startTimer(Duration duration) {
started = true;
print('starting timer');
_timer = Timer.periodic(const Duration(seconds: 1), ((timer) {
if (timer.tick >= duration.inSeconds) {
print(elapsedSeconds);
timer.cancel();
started = false;
}

View File

@ -0,0 +1,33 @@
import 'package:flutter/material.dart';
class WorkoutProvider extends ChangeNotifier {
final List<String> _workoutPhases = [
'Warm-Up',
'High Intensity',
'Low Intensity',
'High Intensity',
'Low Intensity',
'High Intensity',
'Low Intensity',
'High Intensity',
'Cool-down'
];
final Map<String, Duration> _phasesDuration = {
'Warm-Up': const Duration(seconds: 5),
'High Intensity': const Duration(seconds: 4),
'Low Intensity': const Duration(seconds: 3),
'Cool-down': const Duration(seconds: 5)
};
int _workoutPhaseIndex = 0;
String get currentPhase => _workoutPhases[_workoutPhaseIndex];
Duration get currentPhaseDuration =>
_phasesDuration[currentPhase] ?? const Duration(seconds: 0);
void nextPhase() {
_workoutPhaseIndex < _workoutPhases.length
? _workoutPhaseIndex += 1
: _workoutPhaseIndex = 0;
//notifyListeners();
}
}

View File

@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:smoke_cess_app/providers/workout_provider.dart';
import 'package:smoke_cess_app/widgets/timer_widget.dart';
import '../providers/timer_provider.dart';
class WorkoutTimerWidget extends StatelessWidget {
const WorkoutTimerWidget({super.key});
@override
Widget build(BuildContext context) {
TimerProvider timerProvider = context.watch<TimerProvider>();
WorkoutProvider workoutProvider = context.watch<WorkoutProvider>();
if (timerProvider.elapsedSeconds ==
workoutProvider.currentPhaseDuration.inSeconds) {
print('Timer abgelaufen');
workoutProvider.nextPhase();
timerProvider.startTimer(workoutProvider.currentPhaseDuration);
}
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(workoutProvider.currentPhase),
TimerWidget(duration: workoutProvider.currentPhaseDuration)
],
);
}
}