Custom Timer with 30 second limit in flutter

 


custom_timer.dart

import 'dart:async';

class TimerService {
  bool _isRunning = false;
  bool _isPaused = false;
  late int _remainingSeconds;
  late Timer _timer;
  Function(bool) onTimerComplete;

  TimerService({required this.onTimerComplete});

  bool get isRunning => _isRunning;
  bool get isPaused => _isPaused;
  int get remainingSeconds => _remainingSeconds;

  void start(int seconds) {
    if (!_isRunning) {
      _remainingSeconds = seconds;
      _isRunning = true;
      _isPaused = false;
      _timer = Timer.periodic(Duration(seconds: 1), _tick);
    }
  }

  void pause() {
    if (_isRunning && !_isPaused) {
      _timer.cancel();
      _isPaused = true;
    }
  }

  void resume() {
    if (_isRunning && _isPaused) {
      _timer = Timer.periodic(Duration(seconds: 1), _tick);
      _isPaused = false;
    }
  }

  void _tick(Timer timer) {
    _remainingSeconds++;
    if (_remainingSeconds >= 30) {
      _isRunning = false;
      _timer.cancel();
      if (onTimerComplete != null) {
        onTimerComplete(true);
      }
    }
  }

  void dispose() {
    if (_isRunning) {
      _timer.cancel();
    }
  }
}


HomeScreen.dart


where you want to show it, put this

Text(
                                "00:${timerService.remainingSeconds.toString().padLeft(2, '0')}",
                                style: TextStyle(fontSize: 24),
                              )


 @override
  void initState() {
    timerService = TimerService(
      onTimerComplete: (bool timerCompleted) {
        if (timerCompleted) {
          // Handle timer completion as needed
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text("Timer has completed!"),
            ),
          );
        }
      },
    );

    Timer.periodic(Duration(seconds: 1), (timer) {
      setState(() {}); // Trigger a rebuild of the widget
    });
    super.initState();
  }














Comments