如何使拍打倒计时? [英] How to make a countdown in flutter?

查看:51
本文介绍了如何使拍打倒计时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得一个定时器小部件,该定时器小部件在时间到期后导航到另一个屏幕,或者在滑动手势后重新启动?

How can I achieve a Timer Widget that navigates to another screen after the time expired or it restarts itself after, lets say a swiping gesture?

推荐答案

Flutter具有一个名为 RestartableTimer 的类,该类从 Timer 扩展而来。设置了计时器后,它会得到一个 Duration 元素和一个回调方法。

Flutter has a class called RestartableTimer which extends from Timer. It get's a Duration element and a callback method when the timer is set.

要重新启动它,只需重置即可。这是完成所有这些操作的代码片段。您只需将代码放在相关位置即可。

When you want to restart it, you can simply reset it. Here is the code snippet to go through all of this. You can just put the code to the related place.

//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();

这篇关于如何使拍打倒计时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆