简体   繁体   中英

Flutter Countdown End Time

I'm trying to create a 2 minute counter. The purpose of this counter is to start again when the time expires and make a request to the server. I created a countdown widget for this, but I couldn't make it start again and send a request to the server when the time expires. I will be glad if you help

Countdown Widget

class Countdown extends AnimatedWidget {
  Countdown({Key? key, required 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).toString().padLeft(2, '0')}';

    return Text(
      "$timerText ",
      style: TextStyle(
        fontSize: 14.sp,
        color: AppColors.DARK_GREY,
      ),
    );
  }
}

Use countdown widget

Countdown(
                  animation: StepTween(
                    begin: controller
                        .levelClock.value, // THIS IS A USER ENTERED NUMBER
                    end: 0,
                  ).animate(controller.animationController!),
                ),

page controller:

  RxInt levelClock = 120.obs;
  AnimationController? animationController;

  @override
  void onInit() {
    animationController = AnimationController(
        vsync: this, duration: Duration(seconds: levelClock.value));

    animationController!.forward();
    super.onInit();
  }

Rather than making a counter and doing countdown you can use Timer. You can refer below code.

import 'dart:async';

Timer timer = Timer.periodic(const Duration(minutes: 2), (Timer t) {
          //do whatever you want
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM