计时器停止抖动时如何进入新屏幕

时间:2020-09-18 05:34:31

标签: flutter dart

我正在构建一个带有动画计时器的Flutter应用程序。计时器完成后,我希望它将其重定向到其他屏幕。但是,当计时器停止运行时(即变为0:00),它将停留在同一屏幕上。请帮我解决一下这个。 以下是代码:

class Countdown extends AnimatedWidget {

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

  @override
  build(BuildContext context) {
    Duration clockTimer = Duration(seconds: animation.value);

    String timerText =
        '${clockTimer.inMinutes.remainder(60).toString()}:${(clockTimer.inSeconds.remainder(60) % 60).toString().padLeft(2, '0')}';

    if(clockTimer.inMinutes.remainder(60) < 1){
      return Text(
        "$timerText",
        style: TextStyle(
          fontSize: 60,
          color: Colors.red,
        ),
      );
    }
    else if(clockTimer.inMinutes.remainder(60) < 1 && (clockTimer.inSeconds.remainder(60) % 60) <= 00){
      Navigator.of(context).pushReplacement(MaterialPageRoute(
        builder: (context) => resultpage(),
      ));
    }
    else{
      return Text(
        "$timerText",
        style: TextStyle(
          fontSize: 60,
          color: Theme.of(context).primaryColor,
        ),
      );
    }
  }
}

小部件和功能:

class _quizpageState extends State<quizpage> with SingleTickerProviderStateMixin {

  
  AnimationController _controller;

  

  int i = 0;

  Timer _timer;
  int _start = 300;
  String time = "300";
  int flag = 0;

  final answercontroller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitDown, DeviceOrientation.portraitUp
    ]);
    starttimer();
    // TODO: implement build
    return WillPopScope(
      onWillPop: () {
        return showDialog(
            context: context,
            builder: (context) =>
                AlertDialog(
                  title: Text(
                      "Game"
                  ),
                  content: Text(
                      "You cannot go back at this stage"
                  ),
                  actions: <Widget>[
                    FlatButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text(
                          "OK"
                      ),
                    )
                  ],
                )
        );
      },
      child: Scaffold(
        backgroundColor: Colors.blue,
        body: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: SizedBox(
            height: 600,
            child: Column(
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: Align(
                    alignment: Alignment.bottomCenter,
                    child: Countdown(
                      animation: StepTween(
                        begin: 2 * 60,
                        end: 0,
                      ).animate(_controller),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
  
  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;
              }
              time = _start.toString();
            },
          ),
    );
  }


    @override
      void initState() {
        SystemChrome.setEnabledSystemUIOverlays([]);
        super.initState();
        _controller =
            AnimationController(vsync: this, duration: Duration(minutes: 5));
        _controller.forward();
    }
    
    @override
  void dispose() {
    _timer.cancel();
    _controller.dispose();
    super.dispose();
  }

}

0 个答案:

没有答案