Flutter 在颤振中设置反向倒计时
我想使用下面的示例代码作为每周报价,但我需要两件事: 1-在我的计时器中添加计数日 2-我想反向启动计时器,例如,我想使用它一周,这意味着7天倒计时,当它完成后,在7天内再次自动启动。。。我怎样才能用这段代码做到这一点。 多谢各位Flutter 在颤振中设置反向倒计时,flutter,timer,countdown,Flutter,Timer,Countdown,我想使用下面的示例代码作为每周报价,但我需要两件事: 1-在我的计时器中添加计数日 2-我想反向启动计时器,例如,我想使用它一周,这意味着7天倒计时,当它完成后,在7天内再次自动启动。。。我怎样才能用这段代码做到这一点。 多谢各位 import 'package:flutter/material.dart'; import 'dart:async'; class TimerWeek extends StatefulWidget { @override _TimerWeekState cr
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,
),
),
],
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“dart:async”;
类TimerWeek扩展StatefulWidget{
@凌驾
_TimerWeekState createState()=>\u TimerWeekState();
}
类_TimerWeekState扩展状态{
静态常数持续时间=常数持续时间(秒:1);
int=0;
bool isActive=false;
定时器;
void handleTick(){
如果(isActive){
设置状态(){
第二次通过=第二次通过+1;
});
}
}
@凌驾
小部件构建(构建上下文){
如果(计时器==null){
定时器=定时器。周期(持续时间,(定时器t){
handleTick();
});
}
int seconds=secondPassed%60;
int minutes=secondPassed~/60;
整小时=秒通过~/(60*60);
返回容器(
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
划船(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
LabelText(标签:“”,值:hours.toString().padLeft(2,'0')),
LabelText(标签:“”,值:minutes.toString().padLeft(2,'0')),
LabelText(标签:“”,值:seconds.toString().padLeft(2,'0')),
],
),
尺寸箱(高度:30),
容器(
宽度:200,
身高:47,
页边空白:仅限边集(前30页),
孩子:升起按钮(
颜色:颜色。粉红色[200],
形状:圆形矩形边框(
边界半径:边界半径。圆形(25)),
子项:文本(isActive?'STOP':'START'),
已按下:(){
设置状态(){
isActive=!isActive;
});
},
),
)
],
),
);
}
}
类LabelText扩展了无状态小部件{
LabelText({this.label,this.value});
最终字符串标签;
最终字符串值;
@凌驾
小部件构建(构建上下文){
返回容器(
边缘:边缘组。对称(水平:5),
填充:边缘设置。全部(20),
装饰:盒子装饰(
边界半径:边界半径。圆形(25),
颜色:Colors.teal,
),
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
正文(
“$value”,
样式:TextStyle(
颜色:Colors.white,fontSize:12,fontwweight:fontwweight.bold),
),
正文(
“$label”,
样式:TextStyle(
颜色:颜色。白色70,
),
),
],
),
);
}
}
如果我错了,请纠正我,但你要找一个1周倒计时计时器?此时,您的计时器中似乎已经有小时、分钟和秒,因此您只需添加天、一周,然后计算时间间隔
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);
通过这种方式,您将拥有一周中剩下的秒、分钟、小时和天,然后只需添加一个IF语句块来测试timeLeft=0,然后再次将计时器重置为604800
如果我误解了你的要求,请随时告诉我,我会尽力回答的