每当新数据变为0时刷新Flutter CountDown

时间:2019-11-29 17:34:59

标签: flutter dart

现在我有一个CountDownTimer类,如下所示:

{'A': [12, 3], 'm': [8, 15], 'a': [19, 23], 'z': [7, 6], 'i': [5, 4] , ...}

还有一个executionToReps函数,它从锻炼中返回整数列表(Workout.exercise是一个数组数组,每个数组都有一个名称(在[0]中)和一个字符串,用于表示秒或分钟(在[1中])

class CountDownTimer extends StatefulWidget {
    final Workout workout;
    CountDownTimer(this.workout);

    @override
    State<CountDownTimer> createState() {
      return _CountDownTimerState(this.workout);
    }
}

class _CountDownTimerState extends State<CountDownTimer> with TickerProviderStateMixin {
    AnimationController controller;
    int index = 0;
    List<dynamic> list;

    Workout workout;
    _CountDownTimerState(this.workout);

  String get timerString {
    Duration duration = controller.duration * controller.value;
    return '${duration.inMinutes}:${(duration.inSeconds % 60).toString().padLeft(2, '0')}';
  }
  @override
  void initState() {
    super.initState();
    this.list = workoutToReps();

    controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: list[index])
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white10,
      body:
      AnimatedBuilder(
          animation: controller,
          builder: (context, child) {
            return Stack(
              children: <Widget>[
                Align(
                  alignment: Alignment.bottomCenter,
                ),
                Padding(
                  padding: EdgeInsets.all(8.0),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Expanded(
                        child: Align(
                          alignment: FractionalOffset.center,
                          child: AspectRatio(
                            aspectRatio: 1.0,
                            child: Stack(
                              children: <Widget>[
                                Align(
                                  alignment: FractionalOffset.center,
                                  child: Column(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceEvenly,
                                    crossAxisAlignment:
                                    CrossAxisAlignment.center,
                                    children: <Widget>[
                                      Text(
                                        this.workout.exercises[index][0],
                                        style: TextStyle(
                                            fontSize: 20.0,
                                            color: Colors.white),
                                      ),
                                      Text(
                                        timerString,
                                        style: TextStyle(
                                            fontSize: 112.0,
                                            color: Colors.white),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                      AnimatedBuilder(
                          animation: controller,
                          builder: (context, child) {
                            return FloatingActionButton.extended(
                                onPressed: () {
                                  if (controller.isAnimating)
                                    controller.stop();
                                  else {
                                    controller.reverse(
                                        from: controller.value == 0.0
                                            ? 1.0
                                            : controller.value);
                                  }
                                },
                                icon: Icon(controller.isAnimating
                                    ? Icons.pause
                                    : Icons.play_arrow),
                                label: Text(
                                    controller.isAnimating ? "Pause" : "Play"));
                          }),
                    ],
                  ),
                ),
              ],
            );
          }),
    );
  }
}

我现在想做的是CountDownTimer在整数列表中滚动,每次计数到0时,将值更改为列表中的下一个并重新开始倒计时

0 个答案:

没有答案