flutter 中是否有任何 class 可以像 Handler 一样工作?
Is there any class in flutter which will work same like Handler?
谁能告诉我,flutter 中的 handler 是什么?我想实现一个持续 5 秒的闪屏,然后会显示另一个屏幕。
我认为没有类似于 Handler
class 的东西,但您可以只使用 Future.delayed
并在 build()
中呈现不同的 UI 取决于 showSplash
:
showSplash = true;
new Future.delayed(const Duration(seconds: 5), () {
setState(() => showSplash = false);
});
Handler.postDelayed() -- 用于在特定时间后工作
我们可以使用 Future.postDelayed
(如 Günter 所回答),也可以使用 Timer
class。
Timer(Duration(seconds: 5), () {
// 5 seconds have past, you can do your work
}
Handler.post() -- 用于在特定时间间隔后继续工作
我们可以使用 Timer.periodic
功能,例如
Timer.periodic(Duration(seconds: 5), (_) {
// this code runs after every 5 second. Good to use for Stopwatches
});
new Future.delayed(new Duration(seconds: 5), () {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => NewScreen()),
(Route route) => false);
});
其中 NewScreen
是您要显示的屏幕,它将显示一个新屏幕。
谁能告诉我,flutter 中的 handler 是什么?我想实现一个持续 5 秒的闪屏,然后会显示另一个屏幕。
我认为没有类似于 Handler
class 的东西,但您可以只使用 Future.delayed
并在 build()
中呈现不同的 UI 取决于 showSplash
:
showSplash = true;
new Future.delayed(const Duration(seconds: 5), () {
setState(() => showSplash = false);
});
Handler.postDelayed() -- 用于在特定时间后工作
我们可以使用 Future.postDelayed
(如 Günter 所回答),也可以使用 Timer
class。
Timer(Duration(seconds: 5), () {
// 5 seconds have past, you can do your work
}
Handler.post() -- 用于在特定时间间隔后继续工作
我们可以使用 Timer.periodic
功能,例如
Timer.periodic(Duration(seconds: 5), (_) {
// this code runs after every 5 second. Good to use for Stopwatches
});
new Future.delayed(new Duration(seconds: 5), () {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => NewScreen()),
(Route route) => false);
});
其中 NewScreen
是您要显示的屏幕,它将显示一个新屏幕。