暂停颤振倒数计时器 [英] Pause Flutter Countdown Timer
问题描述
我一直在玩Dart Timer Class,让它以最基本的形式工作,但是我一直在尝试向其添加暂停功能。我查看了他们的文档,但是他们对Timer类的了解不多...
I have been playing with Dart Timer Class that I got it to work in its very basic form, however I am stuck trying to add a pause function to it. I have looked into their documentations, but their is not much about their Timer class...
有什么方法可以暂停和恢复点击时的计时器/倒计时吗?这是我到目前为止所取得的成就:
Is there any way I can pause and resume the timer/countdown on click? This is what I have achieved so far:
import 'package:flutter/material.dart';
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Timer _timer;
int _start = 10;
void startTimer() {
const oneSec = const Duration(seconds: 1);
_timer = new Timer.periodic(
oneSec,
(Timer timer) => setState(() {
if (_start < 1) {
timer.cancel();
} else {
_start = _start - 1;
}
}));
}
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")
],
));
}
}
推荐答案
有没有内置的 pause
函数,因为 Timer
类主要用于计划以后的代码块。
There is no built-in pause
function, since the Timer
class is mainly intended for scheduling blocks of code for later.
您的用例是什么? Stopwatch
类具有暂停和恢复功能。
What is your use case? The Stopwatch
class has pause and resume funtionality.
这篇关于暂停颤振倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!