简体   繁体   中英

make reverse countdown timer in flutter

i want to use below sample code for section Weekly Offer but i need two things for this:

1- add count day in my timer 2- i want to start timer in reverse for example i want to use it for a week that means 7 days count down and when it has been finished , automatically start in 7 days again... how can i do it with this code. Thank you.

import 'package:flutter/material.dart';
import 'dart:async';

 class TimerWeek extends StatefulWidget {
 @override
 _TimerWeekState createState() => _TimerWeekState();
 }

class _TimerWeekState extends State<TimerWeek> {
 static const duration = const Duration(seconds: 1);

 int secondPassed = 0;
 bool isActive = false;

 Timer timer;
 void handleTick() {
  if (isActive) {
   setState(() {
     secondPassed = secondPassed + 1;
   });
   }
  }

@override
Widget build(BuildContext context) {
if (timer == null) {
  timer = Timer.periodic(duration, (Timer t) {
    handleTick();
  });
}
int seconds = secondPassed % 60;
int minutes = secondPassed ~/ 60;
int hours = secondPassed ~/ (60 * 60);
return Container(
  child: Column(
    mainAxisSize: MainAxisSize.min,
    children: <Widget>[
      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          LabelText(label: '', value: hours.toString().padLeft(2, '0')),
          LabelText(label: '', value: minutes.toString().padLeft(2, '0')),
          LabelText(label: '', value: seconds.toString().padLeft(2, '0')),
        ],
      ),
      SizedBox(height: 30),
      Container(
        width: 200,
        height: 47,
        margin: EdgeInsets.only(top: 30),
        child: RaisedButton(
          color: Colors.pink[200],
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(25)),
          child: Text(isActive ? 'STOP' : 'START'),
          onPressed: () {
            setState(() {
              isActive = !isActive;
            });
          },
        ),
      )
    ],
  ),
);
}
}
class LabelText extends StatelessWidget {
 LabelText({this.label, this.value});

 final String label;
 final String value;

 @override
 Widget build(BuildContext context) {
  return Container(
   margin: EdgeInsets.symmetric(horizontal: 5),
   padding: EdgeInsets.all(20),
    decoration: BoxDecoration(
     borderRadius: BorderRadius.circular(25),
     color: Colors.teal,
   ),
   child: Column(
     mainAxisSize: MainAxisSize.min,
     children: <Widget>[
       Text(
         '$value',
         style: TextStyle(
             color: Colors.white, fontSize: 12, fontWeight: FontWeight.bold),
       ),
      Text(
        '$label',
        style: TextStyle(
          color: Colors.white70,
        ),
      ),
    ],
  ),
);
}
}

Well correct me if I'm wrong, but you're looking for a 1 week count-down timer? You seem to already have hours, minutes, and seconds in your timer at this point, so you'd just add days, a week, and calculate timeLeft from there.

int days = secondPassed ~/ (86400); 
int const week = 604800;  // number of seconds in a week
int timeLeft = week - secondPassed;
int daysLeft = timeLeft ~/ (86400);
int hoursLeft = (timeLeft - (daysLeft * 86400)) ~/ (3600); //number of seconds in an hour
int minutesLeft = (timeLeft - (daysLeft * 86400) - (hoursLeft * 3600)) ~/ (60);
int secondsLeft = (timeLeft - (daysLeft * 86400) - (hoursLeft * 3600) - (minutesLeft * 60)) % (60);

In this way you'd have the seconds, minutes, hours, and days left of the week, and then just add an IF statement block to test if timeLeft = 0, and then reset the timer to 604800 again.

If I misunderstand what you're asking, feel free to let me know, and I'll try to answer that!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM