简体   繁体   中英

Stop timer when changing page with page view and bottom navigation

I use the BottomNavigationBar that switches between the display of 5 different pages. Changing the pages is done with the help of PageController together with the PageView widget.

在此处输入图像描述

Within the pages, there are timers that run when entering and exiting the page and pull data from the server every X time by using Timer.periodic .

Because of the use of PageView , the pages are built at the same time and all the timers are running, how can the timers be canceled when moving from page to page through each page by itself?

you could listen to the PageController and stop the timer. Like this

pageController.addListener(() { 
      if(controller.page == 1){
        timer.cancel();
      }
    });

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