2023-02-26 19:52:45 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class TimerProvider extends ChangeNotifier {
|
|
|
|
Timer? _timer;
|
|
|
|
bool started = false;
|
|
|
|
int get elapsedSeconds => _timer != null ? _timer!.tick : 0;
|
|
|
|
|
|
|
|
void startTimer(Duration duration) {
|
2023-03-03 18:26:44 +01:00
|
|
|
print('started');
|
2023-02-26 19:52:45 +01:00
|
|
|
started = true;
|
|
|
|
_timer = Timer.periodic(const Duration(seconds: 1), ((timer) {
|
|
|
|
if (timer.tick >= duration.inSeconds) {
|
|
|
|
timer.cancel();
|
|
|
|
started = false;
|
|
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:07:47 +01:00
|
|
|
void stopTimer() {
|
|
|
|
started = false;
|
|
|
|
_timer?.cancel();
|
2023-03-03 15:02:27 +01:00
|
|
|
_timer = null;
|
2023-02-28 15:07:47 +01:00
|
|
|
}
|
2023-03-03 17:21:27 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2023-03-03 18:26:44 +01:00
|
|
|
print('disposed');
|
|
|
|
started = false;
|
2023-03-03 17:21:27 +01:00
|
|
|
_timer?.cancel();
|
2023-03-03 18:26:44 +01:00
|
|
|
_timer = null;
|
2023-03-03 17:21:27 +01:00
|
|
|
super.dispose();
|
|
|
|
}
|
2023-02-26 19:52:45 +01:00
|
|
|
}
|