简体   繁体   English

使用页面视图和底部导航更改页面时停止计时器

[英]Stop timer when changing page with page view and bottom navigation

I use the BottomNavigationBar that switches between the display of 5 different pages.我使用在 5 个不同页面的显示之间切换的BottomNavigationBar Changing the pages is done with the help of PageController together with the PageView widget.更改页面是在PageControllerPageView小部件的帮助下完成的。

在此处输入图像描述

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 .在页面中,有计时器在进入和退出页面时运行,并使用Timer.periodic每 X 次从服务器拉取数据。

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?因为使用PageView ,页面是同时构建的,所有的定时器都在运行,如何在页面移动时通过自己的页面移动到页面时取消定时器?

you could listen to the PageController and stop the timer.您可以收听PageController并停止计时器。 Like this像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter - 底部导航栏更改页面,但未选择图标 - Flutter - Bottom Navigation Bar changing page, but not icon selected Flutter 持久化底部导航栏和页面视图替换方法 - Flutter persistent bottom navigation bar and page view replacement approach 单击底部导航栏项(并加载相关页面)时隐藏底部导航栏 - Flutter - Hide the bottom navigation bar when clicking on one bottom navigation bar item (and loading related page) - Flutter 在 flutter 中使用底部导航选项卡时显示页面时出现问题 - Problem displaying a page when using bottom navigation tab in flutter Flutter 页面重建时重建底部导航栏 - Flutter rebuild bottom navigation bar when page is rebuilt 在底部导航栏上单击图标时不会导航到其他页面 - On bottom navigation bar when clicking on icons it is not navigating to other page 导航回带有底部导航栏的页面时,颤动传递底部导航栏选定的索引 - Flutter pass bottom navigation bar selected index when navigating back to page with bottom nav bar Flutter:带有嵌套导航的底部导航栏和更改选项卡时恢复根页面 - Flutter: Bottom Navigation bar with nested navigation and Restoring the Root page when tabs are changed 如何在 flutter 中的页面中添加底部导航项 - How to add a bottom navigation item to a page in flutter 底部导航栏 flutter 详情页 - bottom navigation bar flutter detail page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM