简体   繁体   中英

How to set countdown timer for all page in flutter

I have 3 screens (A, B, and C). At the A screen, I have a button to countdown a timer but what makes me confused is... Is there a way to set global count down in each page. So what I mean is.. the time will keep runnings when I change to another page. Here is the part of the code of countdown timer

Timer _timer;
  startTimer() {
    const oneSec = const Duration(seconds: 1);
    int dummyTimer = timerNotifier.value.start;
    print("dummyTimer $dummyTimer");
    widget.dataRegis.value = RegisterUser(
        timerStart: widget.dataRegis.value.timerStart,
        empId: widget.dataRegis.value.empId,
        idDeviceUser: widget.dataRegis.value.idDeviceUser,
        agreement: widget.dataRegis.value.agreement,
        isWaiting: true);
    _timer = new Timer.periodic(
      oneSec,
      (Timer timer) {
        if (timerNotifier.value.start == 0)
          timer.cancel();
        else {
          dummyTimer--;
          timerNotifier.value = TimerModel(start: dummyTimer);
          widget.dataRegis.value = RegisterUser(
              timerStart: dummyTimer,
              empId: widget.dataRegis.value.empId,
              idDeviceUser: widget.dataRegis.value.idDeviceUser,
              agreement: widget.dataRegis.value.agreement,
              isWaiting: true);
        }
      },
    );
  }

You can use libraries like provider to keep Timer there so they can be accessible for all Widget

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