如何将传递的值放入构造中,制作一个四舍五入到小数点后一位并显示在我的 RaisedButton 子文本中的计时器?我试过但没有运气。我设法使用一个简单的 Timer 使回调函数工作,但没有周期性,也没有在文本中实时更新值......
import 'package:flutter/material.dart';
import 'dart:ui';
import 'dart:async';
class TimerButton extends StatefulWidget {
final Duration timerTastoPremuto;
TimerButton(this.timerTastoPremuto);
@override
_TimerButtonState createState() => _TimerButtonState();
}
class _TimerButtonState extends State<TimerButton> {
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(5.0),
height: 135.0,
width: 135.0,
child: new RaisedButton(
elevation: 100.0,
color: Colors.white.withOpacity(.8),
highlightElevation: 0.0,
onPressed: () {
int _start = widget.timerTastoPremuto.inMilliseconds;
const oneDecimal = const Duration(milliseconds: 100);
Timer _timer = new Timer.periodic(
oneDecimal,
(Timer timer) =>
setState(() {
if (_start < 100) {
_timer.cancel();
} else {
_start = _start - 100;
}
}));
},
splashColor: Colors.red,
highlightColor: Colors.red,
//shape: RoundedRectangleBorder e tutto il resto uguale
shape: BeveledRectangleBorder(
side: BorderSide(color: Colors.black, width: 2.5),
borderRadius: new BorderRadius.circular(15.0)),
child: new Text(
"$_start",
style: new TextStyle(fontFamily: "Minim", fontSize: 50.0),
),
),
);
}
}
最佳答案
这是一个使用 Timer.periodic 的示例:
点击按钮时,从 10 到 0 开始倒计时:
import 'dart:async';
[...]
Timer _timer;
int _start = 10;
void startTimer() {
const oneSec = const Duration(seconds: 1);
_timer = new Timer.periodic(
oneSec,
(Timer timer) {
if (_start == 0) {
setState(() {
timer.cancel();
});
} else {
setState(() {
_start--;
});
}
},
);
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title: Text("Timer test")),
body: Column(
children: <Widget>[
RaisedButton(
onPressed: () {
startTimer();
},
child: Text("start"),
),
Text("$_start")
],
),
);
}
结果:
您也可以使用 CountdownTimer来自 quiver.async 的类(class)库,使用更简单:
import 'package:quiver/async.dart';
[...]
int _start = 10;
int _current = 10;
void startTimer() {
CountdownTimer countDownTimer = new CountdownTimer(
new Duration(seconds: _start),
new Duration(seconds: 1),
);
var sub = countDownTimer.listen(null);
sub.onData((duration) {
setState(() { _current = _start - duration.elapsed.inSeconds; });
});
sub.onDone(() {
print("Done");
sub.cancel();
});
}
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title: Text("Timer test")),
body: Column(
children: <Widget>[
RaisedButton(
onPressed: () {
startTimer();
},
child: Text("start"),
),
Text("$_current")
],
),
);
}
编辑:关于按钮点击行为的评论中的问题
上面的代码使用Timer.periodic,每次按钮点击都会启动一个新的定时器,所有这些定时器都会更新相同的_start变量,导致更快的递减计数器。
有多种解决方案可以改变这种行为,具体取决于您想要实现的目标:
Timer.periodic 创建,以便多次单击按钮无效if (_timer != null) {
_timer = new Timer.periodic(...);
}
if (_timer != null) {
_timer.cancel();
_start = 10;
}
_timer = new Timer.periodic(...);
if (_timer != null) {
_timer.cancel();
_timer = null;
} else {
_timer = new Timer.periodic(...);
}
你也可以用这个官方async提供 RestartableTimer 的软件包从 Timer 扩展并添加 reset 方法的类。
所以只需在每个按钮点击时调用 _timer.reset();。
最后,Codepen 现在支持 Flutter 了!所以这里是一个活生生的例子,每个人都可以玩它:https://codepen.io/Yann39/pen/oNjrVOb
关于timer - flutter 倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54610121/
我正在寻找一个用ruby演示计时器的在线示例,并发现了下面的代码。它按预期工作,但这个简单的程序使用30Mo内存(如Windows任务管理器中所示)和太多CPU有意义吗?非常感谢deftime_blockstart_time=Time.nowThread.new{yield}Time.now-start_timeenddefrepeat_every(seconds)whiletruedotime_spent=time_block{yield}#Tohandle-vesleepinteravalsleep(seconds-time_spent)iftime_spent
在Rails中,update_attributes!的逆运算是什么?换句话说,是什么将记录映射到属性散列以重新创建该记录及其所有子记录?答案不是ActiveRecord.attributes,因为它不会递归到子对象中。澄清您是否有以下情况:classFoo然后你可以像这样传递一个散列{"name"=>"afoo","bars_attributes"=>[{"name"=>"abar}...]}到update_attributes。但目前尚不清楚如何为此目的以编程方式轻松生成这样的哈希值。编辑:正如我在评论中提到的,我可以做类似的事情:foo.as_json(:include=>:bar
一、环境变量右键点击我的电脑-属性:然后找到环境变量 1.Android的SDK不在C盘的话需要额外配这个到用户环境变量:ANDROID_HOMED:\AndroidSDK2.然后在系统变量:Path中添加一条这样的值 D:\Flutter\flutter\bin 这个值写flutter包解压的实际地址即可 3.在系统变量中添加两个镜像变量: 变量名:FLUTTER_STORAGE_BASE_URL 变量值:https://storage.flutter-io.cn 变量名:PUB_HOSTED_URL 变量
我正在按照DCI构建新Rails应用程序的行为,但我对将验证放在哪里有一些疑问。传统上,如果您要使用ActiveRecord模型管理您的数据,验证是在继承自AR的特定类中定义的,并且它们似乎适合作为数据层的一部分。然而,在我看来,只在特定角色下进行某些验证是有意义的,并且只有当对象在该上下文中时才应检查它们,在所有其他情况下都将被忽略。这基本上意味着这些验证应该在特定角色上定义,并且当对象在有意义的上下文中使用时,应该使用这些角色模块扩展对象。您认为将这些验证保留在角色上是个好主意吗?如果是这样,您如何声明它们而不污染与对象相同的类的其他实例?如果我想使用ActiveRecord验证,
文章目录一、引言二、Timers1.System.Threading.Timer1.1.简单使用1.2.注意点2.System.Timers.Timer2.1.概述🔺2.2.注意点三、总结一、引言在开发中,会遇到并行处理的需求。有时只需要使用task(底层是创建个线程)来处理一下就好了。而有时则在并行处理的基础上还有时间的要求,较常见的就是每隔一定时间处理一次。当然,这用task肯定可以实现,但是时间这块得自己控制,无疑增加了工作量和不确定性。.NET提供了叫做定时器(timer,也叫计时器)的类,它在并行处理的基础上,带了时间参数的设置,可以满足这一需求。其实本文标题与其叫C#定时器,不如叫
我将一个旧脚本迁移到一个新的CentOSbox并在运行脚本时收到以下消息:Faraday:youmaywanttoinstallsystem_timerforreliabletimeouts它是警告吗?system_timer是什么?gem? 最佳答案 这是一颗gem。不过,您应该不再需要它了,因为它只支持Ruby1.8及更早版本(Ruby1.8已正式弃用)。来自http://ph7spot.com/musings/system-timer:Update:system_timerisonlyrelevantifyouarerunni
我在控制台服务应用中使用system.threading.timer,并尝试每天同时制作计时器。最初,如果我在时间之前启动该应用程序,我会很好。就像我的时间是10:05,我从10:00启动该应用程序,我们很好。但是,如果我从10:06开始,我就不知道如何告诉时间台下24小时。谢谢你的帮助!publicvoidSetUpTimer(TimeSpanalertTime){DateTimecurrent=DateTime.Now;TimeSpantimeToGo=alertTime-current.TimeOfDay;if(timeToGo{EventLog.WriteEntry("MhyApp",
我想计算出特定函数使用了多少毫秒。所以我四处寻找,但找不到以毫秒精度在Ruby中获取时间的方法。你是怎么做到的?在大多数编程语言中,它就像start=now.millisecondsmyfunction()end=now.millisecondstime=end-start 最佳答案 您可以使用ruby的Time类。例如:t1=Time.now#processing...t2=Time.nowdelta=t2-t1#inseconds现在,delta是一个float对象,您可以获得类将提供的尽可能精细的结果。
我正在尝试绘制一个分类的多轴排名柱形图。排名第一的应该是最高的列,最低的排名应该是最短的。基本上我希望条形的高度是倒数。非常接近:varplayer_name_array=["AaronRodgers","AndrewLuck","DrewBrees","RussellWilson","PeytonManning","RyanTannehill","TonyRomo","MattRyan","CamNewton","BenRoethlisberger","EliManning","PhilipRivers","ColinKaepernick","TeddyBridgewater","M
加载网页后,我会通过控制台插入一些Javscript。我想知道我是否有可能使用Javascript或jQuery重新加载页面(而不是从缓存中),同时保持我正在运行的setInterval。我熟悉location.reload(),但这会终止它。 最佳答案 当您重新加载页面时,包括所有正在运行的JS在内的整个页面上下文将被完全破坏。重新加载其主机页面时,您不能保持setInterval()运行。您可以为新页面创建一个信号,以使用cookie、查询参数或本地存储值(查询参数可能是最合适的)再次开始间隔。如果采用这种方式,则需要对页面进行