handle stop

main
Julian Gegner 2023-02-28 15:07:47 +01:00
parent 4144bbaf69
commit c1cf3cce30
3 changed files with 17 additions and 2 deletions

View File

@ -18,5 +18,8 @@ class TimerProvider extends ChangeNotifier {
}));
}
void stopTimer() => _timer?.cancel();
void stopTimer() {
started = false;
_timer?.cancel();
}
}

View File

@ -8,6 +8,7 @@ class WorkoutProvider extends ChangeNotifier {
final AudioPlayer _audioPlayer = AudioPlayer();
final Source _finishedSoundSource = AssetSource('finish.mp3');
final Source _beepSoundSource = AssetSource('beep.mp3');
bool isWorkoutStarted = false;
WorkoutProvider(this.timerProvider);
@ -57,10 +58,19 @@ class WorkoutProvider extends ChangeNotifier {
}
void startWorkout() {
isWorkoutStarted = true;
_audioPlayer.play(_beepSoundSource);
_audioPlayer.onPlayerComplete.listen((event) {
_audioPlayer.play(_phaseSongSources[currentPhase]!);
timerProvider.startTimer(currentPhaseDuration);
});
}
void stopWorkout() {
isWorkoutStarted = false;
_workoutPhaseIndex = 0;
_audioPlayer.stop();
timerProvider.stopTimer();
notifyListeners();
}
}

View File

@ -25,7 +25,9 @@ class WorkoutTimerWidget extends StatelessWidget {
Text(workoutProvider.currentPhase),
TimerWidget(duration: workoutProvider.currentPhaseDuration),
ElevatedButton(
onPressed: () => workoutProvider.startWorkout(),
onPressed: !workoutProvider.isWorkoutStarted
? () => workoutProvider.startWorkout()
: () => workoutProvider.stopWorkout(),
child: Text(timerProvider.started ? 'Stop' : 'Start'))
],
);