Home > Enterprise >  Flutter Programmable Timer
Flutter Programmable Timer

Time:05-18

i want to build wedding app for my school project. I cant build programmable timer. Users should set the timer like 10week later and timer start to countdown to 9week 23hours etc. Can u help me ?

CodePudding user response:

There are several steps that you should follow to complete this assignment:

  1. You have to let user choose a date and/or a time.
  2. You have to calculate the remaining String (x years, x months, x days, etc..)
  3. Show it to the user.
  4. Begin a timer, that will update the UI.
//Timer

Timer.periodic(const Duration(seconds: 1), (timer) {
  //every 1 second, the state will be updated
  setState(() {});
});

//remainingTime calculator
//you have to put this string to a Text widget
String get  _calculateRemainingTime{
    Duration remainingDuration = widget.date.difference(DateTime.now());

    if(remainingDuration.inSeconds <= 0){
      return 'Time has come';
    }

    int remainingSeconds = remainingDuration.inSeconds % 60;

    int remainingMinutes = remainingDuration.inMinutes % 60;

    int remainingHours = remainingDuration.inHours % 24;

    int remainingDays = remainingDuration.inDays;

    int remainingMonths = remainingDays ~/ 30;

    int remainingYears = remainingMonths ~/ 12;

    remainingDays = remainingDays % 30;

    remainingMonths = remainingMonths % 12;


    return '${remainingYears != 0 ? remainingYears.toString()   ' years' : ''} ${remainingMonths != 0 ? remainingMonths.toString()   ' months' : ''} ${remainingDays != 0 ? remainingDays.toString()   ' days' : ''} ${remainingHours != 0 ? remainingHours.toString()   ' hours' : ''} ${remainingMinutes != 0 ? remainingMinutes.toString()   ' minutes' : ''} ${remainingSeconds != 0 ? remainingSeconds.toString()   ' seconds' : ''}';
  }

I have an example on my github page: Flutter Timer Example

if you need further help, tell me.

  • Related