我如何才能获得一个定时器小部件,该定时器小部件在时间到期后导航到另一个屏幕,或者在滑动手势后重新启动?
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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句