简体   繁体   English

定时器.周期性颤振,

[英]Timer.periodic flutter,

@override
void initState() {
 super.initState();

 bool isLastPage = (_currentPage.round() == sliderArrayList.length - 1 );

 //Trying to build automatic scrollable
 Timer.periodic(Duration(seconds: 5), (Timer timer) {
  if (_pageController.hasClients && !isLastPage) {
    _pageController.nextPage(
      duration: Duration(milliseconds: 200),
      curve: Curves.easeIn);}
  if(isLastPage){
    timer.cancel();
  }
 });
}

I am trying to build a periodic timer which scrolls pages in pageview, flutter after every 5 seconds, however it keeps on scrolling even after the last page.我正在尝试构建一个周期性计时器,它在页面视图中滚动页面,每 5 秒后颤动一次,但是即使在最后一页之后它也会继续滚动。 I have tried to implement the above method if(isLastPage){timer.cancel();} and it doesnt work.我试图实现上述方法if(isLastPage){timer.cancel();}但它不起作用。

You've to evaluate that expression everytime timer ticks.您必须在每次计时器滴答时评估该表达式。 Also, Put the isLastPage check before the hasClients check.此外,把isLastPage检查前hasClients检查。

Timer.periodic(Duration(seconds: 5), (Timer timer) {
  final isLastPage = _currentPage.round() == sliderArrayList.length - 1;
  if (isLastPage) {
    timer.cancel();
    return;
  }
  if (_pageController.hasClients) {
    _pageController.nextPage(
      duration: Duration(milliseconds: 200),
      curve: Curves.easeIn);
  }
});

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

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