changed circular progress indicator framerate

main
Julian Gegner 2023-03-04 15:30:21 +01:00
parent dd4ddc566f
commit 3038efcd27
3 changed files with 18 additions and 13 deletions

View File

@ -5,13 +5,18 @@ import 'package:flutter/material.dart';
class TimerProvider extends ChangeNotifier { class TimerProvider extends ChangeNotifier {
Timer? _timer; Timer? _timer;
bool started = false; bool started = false;
int get elapsedSeconds => _timer != null ? _timer!.tick : 0; Duration _duration = const Duration();
int get elapsedSeconds => _duration.inSeconds;
int get elapsedMilliseconds => _duration.inMilliseconds;
final Duration _tickRate = const Duration(milliseconds: 20);
void startTimer(Duration duration) { void startTimer(Duration duration) {
_duration = Duration.zero;
started = true; started = true;
_timer = Timer.periodic(const Duration(seconds: 1), ((timer) { _timer = Timer.periodic(_tickRate, ((timer) {
if (timer.tick >= duration.inSeconds) { _duration += _tickRate;
timer.cancel(); if (elapsedSeconds >= duration.inSeconds) {
_timer?.cancel();
started = false; started = false;
} }
notifyListeners(); notifyListeners();
@ -22,13 +27,12 @@ class TimerProvider extends ChangeNotifier {
started = false; started = false;
_timer?.cancel(); _timer?.cancel();
_timer = null; _timer = null;
_duration = Duration.zero;
} }
@override @override
void dispose() { void dispose() {
started = false; stopTimer();
_timer?.cancel();
_timer = null;
super.dispose(); super.dispose();
} }
} }

View File

@ -54,10 +54,9 @@ class WorkoutProvider extends ChangeNotifier {
Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color']; Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color'];
AssetSource get currentPhaseSource => AssetSource get currentPhaseSource =>
_workoutPhaseSettings[currentPhase]!['source']; _workoutPhaseSettings[currentPhase]!['source'];
/* bool get isWorkoutComplete =>
_workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete; */
void nextPhase() { void nextPhase() {
print('called next phase');
_onCompleteSubscription.cancel(); _onCompleteSubscription.cancel();
_audioPlayer.stop(); _audioPlayer.stop();
if (_workoutPhaseIndex < _workoutPhases.length - 1) { if (_workoutPhaseIndex < _workoutPhases.length - 1) {

View File

@ -74,10 +74,12 @@ class WorkoutTimerWidget extends StatelessWidget {
width: 100, width: 100,
child: CircularProgressIndicator( child: CircularProgressIndicator(
color: workoutProvider.currentPhaseColor, color: workoutProvider.currentPhaseColor,
value: (workoutProvider.currentPhaseDuration.inSeconds value:
.toDouble() - (workoutProvider.currentPhaseDuration.inMilliseconds -
timerProvider.elapsedSeconds) / timerProvider.elapsedMilliseconds)
workoutProvider.currentPhaseDuration.inSeconds)), .toDouble() /
workoutProvider.currentPhaseDuration.inMilliseconds
.toDouble())),
TimerWidget(duration: workoutProvider.currentPhaseDuration), TimerWidget(duration: workoutProvider.currentPhaseDuration),
], ],
), ),