skip to Main Content

I want a timer to decreasing become time from current time. i.e. this is my beginning time ’05:23:23′ and if the the current time or DateTime.now() is going up my time should goes to come down, and if I am going to close the app for 05min and 13sec, and opening the app’ timer should not start from the beginning ’05:23:23′ the up should start from the passed times ’05:18:10′.

3

Answers


  1. If I were you, I would have saved the time in a txt file, then use that for the entire process, because without saving the time somewhere, It won’t be possible for you to do what you want. I heard about Sharedpreferences which is like cache, so I would say that you can also give it a try.

    Login or Signup to reply.
  2. To create a countdown timer that continues to decrease even when the app is closed, you can use a combination of DateTime and SharedPreferences to store the remaining time when the app is closed. here is an example:

    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    import 'dart:async';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: CountdownTimer(),
        );
      }
    }
    
    class CountdownTimer extends StatefulWidget {
      @override
      _CountdownTimerState createState() => _CountdownTimerState();
    }
    
    class _CountdownTimerState extends State<CountdownTimer> {
      Duration initialTime = Duration(hours: 5, minutes: 23, seconds: 23);
      Duration remainingTime;
      Timer timer;
    
      @override
      void initState() {
        super.initState();
        _loadRemainingTime();
      }
    
      void _loadRemainingTime() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        int savedTime = prefs.getInt('remainingTime') ?? initialTime.inSeconds;
        setState(() {
          remainingTime = Duration(seconds: savedTime);
        });
        _startTimer();
      }
    
      void _startTimer() {
        timer = Timer.periodic(Duration(seconds: 1), (timer) {
          setState(() {
            if (remainingTime.inSeconds > 0) {
              remainingTime -= Duration(seconds: 1);
            } else {
              timer.cancel();
            }
          });
        });
      }
    
      void _saveRemainingTime() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        await prefs.setInt('remainingTime', remainingTime.inSeconds);
      }
    
      @override
      void dispose() {
        timer.cancel();
        _saveRemainingTime();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Countdown Timer')),
          body: Center(
            child: Text(
              '${remainingTime.inHours.toString().padLeft(2, '0')}:${(remainingTime.inMinutes % 60).toString().padLeft(2, '0')}:${(remainingTime.inSeconds % 60).toString().padLeft(2, '0')}',
              style: TextStyle(fontSize: 48),
            ),
          ),
        );
      }
    }
    
    • initialTime is set to 05:23:23.
    • remainingTime is initialized based on the stored value or the initial
      time.
    • loadRemainingTime retrieves the remaining time from SharedPreferences and starts the timer.
    • startTimer decreases the remainingTime every second. Saving Remaining
      Time:
    • saveRemainingTime stores the remaining time in SharedPreferences when
      the app is closed.
    • The Text widget displays the remaining time in HH:MM:SS format.
    Login or Signup to reply.
  3. final startTime = DateTime.now();
    final duration = // amount of time for the timer
    final endTime = startTime.add(duration);
    final timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      print(endTime.difference(startTime));
    });
    

    you need to save startTime and endTime in shared preferences to save the timer when the app is closed

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search