暂停颤振倒数计时器

问题描述 投票:0回答:1

我一直在玩Dart Timer Class,我让它以非常基本的形式工作,但是我一直试图为它添加一个暂停功能。我查看了他们的文档,但他们的Timer类并不多...

有没有什么方法可以暂停和恢复点击计时器/倒计时?这是我迄今取得的成就:

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")
          ],
        ));
  }
}
dart flutter
1个回答
1
投票

没有内置的pause函数,因为Timer类主要用于稍后调度代码块。

你的用例是什么? Stopwatch类具有暂停和恢复功能。

© www.soinside.com 2019 - 2024. All rights reserved.