Flutter有一个名为的类RestartableTimer,它从扩展Timer。Duration设置计时器后,它得到一个元素和一个回调方法。
当您想重新启动它时,只需重置它即可。这是完成所有这些操作的代码片段。您可以将代码放在相关位置。
//You need to import this
import 'package:async/async.dart';
// Duration is 5 seconds
Duration _timerDuration = new Duration(seconds: 5);
// Creating a new timer element.
RestartableTimer _timer = new RestartableTimer(_timerDuration, _startNewPage);
fun _startNewPage() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
}
// Restarting the timer
_timer.reset();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2684 次 |
| 最近记录: |