【问题标题】:Add time to countdown timer during while counting down in Flutter在 Flutter 中倒计时期间为倒计时计时器添加时间
【发布时间】:2019-06-04 17:31:37
【问题描述】:

我正在尝试创建一个倒数计时器应用程序,在该应用程序中我可以按下一个按钮来增加更多时间,同时计时器正在主动倒计时,就像每个微波炉都有一个按钮,你可以按下一个按钮来增加一分钟的运行时间它运行时没有停止任何东西的时间。

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new MyApp(),
  ));
}

class Countdown extends AnimatedWidget {
  Countdown({ Key key, this.animation }) : super(key: key, listenable: animation);
  Animation<int> animation;

  @override
  build(BuildContext context){
    return new Text(
      animation.value.toString(),
      style: new TextStyle(fontSize: 150.0),
    );
  }
}

class MyApp extends StatefulWidget {
  State createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> with TickerProviderStateMixin {
  AnimationController _controller;

  static const int kStartValue = 4;

  @override
  void initState() {
    super.initState();
    _controller = new AnimationController(
      vsync: this,
      duration: new Duration(seconds: kStartValue),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.play_arrow),
        onPressed: () => _controller.forward(from: 0.0),
      ),
      body: new Container(
        child: new Center(
          child: new Countdown(
            animation: new StepTween(
              begin: kStartValue,
              end: 0,
            ).animate(_controller),
          ),
        ),
      ),
    );
  }
}

这个来自关于计时器的类似问题的示例对我来说很有意义,并且一直是我的出发点。我知道我需要更改持续时间并用具有适当持续时间的新动画替换动画,但我从来没有得到任何接近我正在寻找的正确行为的东西。

【问题讨论】:

    标签: dart flutter flutter-animation


    【解决方案1】:

    我认为以下是您要完成的任务。点击FloatingActionButton 会增加 5 秒。

    import 'dart:async';
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Countdown Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Countdown Demo'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _timeRemaining = 10;
      Timer _timer;
    
      @override
      void initState() {
        _timer = Timer.periodic(Duration(seconds: 1), (Timer t) => _getTime());
        super.initState();
      }
    
      @override
      void dispose() {
        _timer?.cancel();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Text('$_timeRemaining'),
          ),
          floatingActionButton: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () {
                _timeRemaining += 5;
              }),
        );
      }
    
      void _getTime() {
        setState(() {
          _timeRemaining == 0 ? _timeRemaining = 0 : _timeRemaining--;
        });
      }
    }
    

    【讨论】:

    • 不要忘记在 dispose() 中取消 Timer。
    • 你能详细说明一下吗?
    • 更新为取消Timer 实例。
    • 我没有注意到行为有任何变化,你能解释一下这是什么结果吗?
    • 最好在不再需要资源时将其丢弃。如果带有计时器的 Widget 类在没有先取消计时器的情况下被销毁/处置,则最终可能会导致内存泄漏。
    猜你喜欢
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    相关资源
    最近更新 更多