工具类
class DateCountDownUtil{
static String constructTime(int seconds) {
int day = seconds ~/3600 ~/24;
int hour = seconds ~/ 3600 %24;
int minute = seconds % 3600 ~/ 60;
int second = seconds % 60;
if(day != 0){
return formatTime(day)+'天'+formatTime(hour) + "小时" + formatTime(minute) + "分" + formatTime(second)+'秒';
}else if(hour != 0){
return formatTime(hour) + "小时" + formatTime(minute) + "分" + formatTime(second)+'秒';
}else if(minute !=0){
return formatTime(minute) + "分" + formatTime(second)+'秒';
}else if(second!=0){
return formatTime(second)+'秒';
}else {
return '';
}
}
static String formatTime(int timeNum) {
return timeNum < 10 ? "0" + timeNum.toString() : timeNum.toString();
}
}
使用
Container(
child: WTextView('${DateCountDownUtil.constructTime(widget.info.leftSeconds)}',style: getTextStyle(color: Colors.white,fontSize: ScreenUtil().setSp(32),fontWeight: FontWeight.bold),),
),
_cancelTimer() {
if (_timer != null) {
_timer.cancel();
_timer = null;
}
}
_startTimer() {
const period = const Duration(seconds: 1);
_timer = Timer.periodic(period, (timer) {
setState(() {
widget.info.leftSeconds --;
});
if (widget.info.leftSeconds == 0) {
print('我被取消了 ');
_cancelTimer();
}
});
}
|