cpd_2022_zi/lib/providers/timer_provider.dart

39 lines
874 B
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
class TimerProvider extends ChangeNotifier {
Timer? _timer;
bool started = false;
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) {
_duration = Duration.zero;
started = true;
_timer = Timer.periodic(_tickRate, ((timer) {
_duration += _tickRate;
if (elapsedSeconds >= duration.inSeconds) {
_timer?.cancel();
started = false;
}
notifyListeners();
}));
}
2023-02-28 15:07:47 +01:00
void stopTimer() {
started = false;
_timer?.cancel();
_timer = null;
_duration = Duration.zero;
2023-02-28 15:07:47 +01:00
}
2023-03-03 17:21:27 +01:00
@override
void dispose() {
stopTimer();
2023-03-03 17:21:27 +01:00
super.dispose();
}
}