Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

listener - How can the subpages of pageview monitor page switching in flutter?

PageView.builder(
      itemBuilder: (ctx, index) => pages[index],
      itemCount: pages.length,
      controller: _pageController,
      physics: NeverScrollableScrollPhysics(),
      onPageChanged: (index) {
        setState(() {
          _selectedIndex = index;
        });
      },
    )

when pageChanged,i wish item can know it was been disPlayed or hidden,so I can do something maybe i need a listener? but I don't know how to code,some one help me!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That is nothing but a standard state-passing problem. In your widget building this PageView (say, MyHomePage), you hold your _selectedIndex as your state. On the other hand, you want to pass this state down into some widgets inside page[index]. Thus, you want to pass down a state to a child.

There are really a lot of ways to do so. Try to search state management. You can use a heavy (but beautiful) lib like Mobx. You can also use lightweight things like InheritedWidget.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...