我正在使用这个包 导入'包:flutter_countdown_timer/flutter_countdown_timer.dart';
尝试在页面构建时显示计时器以显示晚上 8 点(2000 年)的倒计时
我知道我必须在构建之上运行超级初始化状态的部分。
但是,如何编写代码以计算到晚上 8 点?
我想这涉及到使用 DateTime.now()
感谢任何帮助。
基本上,我做过这样的事情(根据秒数倒计时),但我一直坚持使用代码对每日计时进行倒计时。
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: CountDownTimer(),
);
}
}
class CountDownTimer extends StatefulWidget {
CountDownTimer({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
AnimationController _controller;
int remainingTime = 180;
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(
seconds:
remainingTime)
);
_controller.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Countdown(
animation: StepTween(
begin: remainingTime,
end: 0,
).animate(_controller),
BRACKETS...
class Countdown extends AnimatedWidget {
Countdown({Key key, this.animation}) : super(key: key, listenable: animation);
Animation<int> animation;
@override
build(BuildContext context) {
Duration clockTimer = Duration(seconds: animation.value);
String timerText =
'${clockTimer.inMinutes.remainder(60).toString()}:${clockTimer.inSeconds.remainder(60).toString().padLeft(2, '0')}';
print('animation.value ${animation.value} ');
print('inMinutes ${clockTimer.inMinutes.toString()}');
print('inSeconds ${clockTimer.inSeconds.toString()}');
print('inSeconds.remainder ${clockTimer.inSeconds.remainder(60).toString()}');
return Text(
"$timerText",
style: TextStyle(
fontSize: 110,
color: Theme.of(context).primaryColor,
),
);
}
}
我做的另一个尝试是使用 导入'包:flutter_countdown_timer/current_remaining_time.dart';
倒计时:
Padding(
padding: EdgeInsets.only(top: height * 0.09),
child: CountdownTimer(
endTime: DateTime(2021, 6, 4, 20, 00, 00).millisecondsSinceEpoch,
widgetBuilder: (_, CurrentRemainingTime time) {
if (time == null) {
return Text('Let\'s Start', style: TextStyle(fontSize: 39));
}
return Text('${time.hours}${time.min}${time.sec}',
style: TextStyle(fontSize: 39));
},
),
),
但它不起作用,它是空的,因此显示“让我们开始吧”。