Flutter:显示分钟和秒的倒数计时器
Flutter: Countdown timer that displays minutes and seconds
我创建了一个倒计时计时器,它将从用户输入的持续时间开始倒计时,以分钟为单位。
倒数计时器在技术上是有效的,但只显示分钟数倒计时。秒数始终保持在 :00。
如何让秒数也显示为倒数计时器的一部分?
这里是相关代码,摘自一个较长的文件。如果需要,我可以添加更多内容。
各个代码片段分解如下:
首先,我有一个 class 实际显示计时器文本并允许对其设置样式:
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*60);
String timerText = '${clockTimer.inMinutes.remainder(60).toString()}:${(clockTimer.inSeconds.remainder(60) % 60).toString().padLeft(2, '0')}';
return Text(
"$timerText",
style: TextStyle(
fontSize: 110,
color: Theme.of(context).primaryColor,
),
);
}
}
我有一个动画控制器如下:
AnimationController _controller;
@override
void dispose(){
_controller.dispose();
super.dispose();
}
void initState() {
super.initState();
@override
_controller = AnimationController(
vsync: this,
duration: Duration(minutes: gameData.levelClock) // gameData.levelClock is a user entered number elsewhere in the applciation
);
}
最后是调用倒计时小部件的代码部分:
Countdown(
animation: StepTween(
begin: gameData.levelClock, // THIS IS A USER ENTERED NUMBER
end: 0,
).animate(_controller),
)
感谢您提供的任何帮助。
所以问题是,当您创建 CountDown
小部件时,您将 begin 设置为一个双精度值,该值是以分钟为单位的计时器值(对于此示例,假设值为 5)。随着动画的继续,它将从 begin
倒数到 end
(在本例中为 0)。在 5 分钟内,它将从 5 变为 0。
然后您使用该值并将其转换为秒,但由于动画的值介于 0 和 5 之间,因此它始终会为您提供 5:00、4:00、3:00, 2:00、1:00 或 0:00。
解决方案是将您的代码更改为
Countdown(
animation: StepTween(
begin: gameData.levelClock * 60, // convert to seconds here
end: 0,
).animate(_controller),
)
然后在倒计时小部件中将 Duration
更改为
Duration clockTimer = Duration(seconds: animation.value);
您可以在 DartPad 上找到工作示例 here
您可以复制粘贴运行下面的完整代码
您的 AnimationController 持续时间需要使用秒
代码片段
_controller = AnimationController(
vsync: this,
duration: Duration(
seconds:
levelClock) // gameData.levelClock is a user entered number elsewhere in the applciation
);
工作演示
完整代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
int _counter = 0;
AnimationController _controller;
int levelClock = 180;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(
seconds:
levelClock) // gameData.levelClock is a user entered number elsewhere in the applciation
);
_controller.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Countdown(
animation: StepTween(
begin: levelClock, // THIS IS A USER ENTERED NUMBER
end: 0,
).animate(_controller),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
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,
),
);
}
}
我创建了一个倒计时计时器,它将从用户输入的持续时间开始倒计时,以分钟为单位。
倒数计时器在技术上是有效的,但只显示分钟数倒计时。秒数始终保持在 :00。
如何让秒数也显示为倒数计时器的一部分?
这里是相关代码,摘自一个较长的文件。如果需要,我可以添加更多内容。
各个代码片段分解如下:
首先,我有一个 class 实际显示计时器文本并允许对其设置样式:
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*60);
String timerText = '${clockTimer.inMinutes.remainder(60).toString()}:${(clockTimer.inSeconds.remainder(60) % 60).toString().padLeft(2, '0')}';
return Text(
"$timerText",
style: TextStyle(
fontSize: 110,
color: Theme.of(context).primaryColor,
),
);
}
}
我有一个动画控制器如下:
AnimationController _controller;
@override
void dispose(){
_controller.dispose();
super.dispose();
}
void initState() {
super.initState();
@override
_controller = AnimationController(
vsync: this,
duration: Duration(minutes: gameData.levelClock) // gameData.levelClock is a user entered number elsewhere in the applciation
);
}
最后是调用倒计时小部件的代码部分:
Countdown(
animation: StepTween(
begin: gameData.levelClock, // THIS IS A USER ENTERED NUMBER
end: 0,
).animate(_controller),
)
感谢您提供的任何帮助。
所以问题是,当您创建 CountDown
小部件时,您将 begin 设置为一个双精度值,该值是以分钟为单位的计时器值(对于此示例,假设值为 5)。随着动画的继续,它将从 begin
倒数到 end
(在本例中为 0)。在 5 分钟内,它将从 5 变为 0。
然后您使用该值并将其转换为秒,但由于动画的值介于 0 和 5 之间,因此它始终会为您提供 5:00、4:00、3:00, 2:00、1:00 或 0:00。
解决方案是将您的代码更改为
Countdown(
animation: StepTween(
begin: gameData.levelClock * 60, // convert to seconds here
end: 0,
).animate(_controller),
)
然后在倒计时小部件中将 Duration
更改为
Duration clockTimer = Duration(seconds: animation.value);
您可以在 DartPad 上找到工作示例 here
您可以复制粘贴运行下面的完整代码
您的 AnimationController 持续时间需要使用秒
代码片段
_controller = AnimationController(
vsync: this,
duration: Duration(
seconds:
levelClock) // gameData.levelClock is a user entered number elsewhere in the applciation
);
工作演示
完整代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
int _counter = 0;
AnimationController _controller;
int levelClock = 180;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(
seconds:
levelClock) // gameData.levelClock is a user entered number elsewhere in the applciation
);
_controller.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Countdown(
animation: StepTween(
begin: levelClock, // THIS IS A USER ENTERED NUMBER
end: 0,
).animate(_controller),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
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,
),
);
}
}