23 lines
523 B
Dart
23 lines
523 B
Dart
|
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) {
|
||
|
started = true;
|
||
|
_timer = Timer.periodic(const Duration(seconds: 1), ((timer) {
|
||
|
if (timer.tick >= duration.inSeconds) {
|
||
|
timer.cancel();
|
||
|
started = false;
|
||
|
}
|
||
|
notifyListeners();
|
||
|
}));
|
||
|
}
|
||
|
|
||
|
void stopTimer() => _timer?.cancel();
|
||
|
}
|