0
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!

1 Answers1

1

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.

ch271828n
  • 15,854
  • 5
  • 53
  • 88
  • @zerongjiang You are welcome :) If my answer helps you, you can click the "accept this answer" at the left of this answer – ch271828n Jan 01 '21 at 06:20