3

I'm trying to access the Page Controller of one screen into the other screen. Let's say there are two screens (Screen A and Screen B)

Screen A Code

class ScreenA extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      body: SafeArea(child: ListView(
        children: [
          for (int i = 0; i < 5; i++) // Both are having same number of loops (Screen A and Screen B)
            TextButton(
              onPressed: (){
                _pageController.animateToPage( // This should redirect them to their respective PageView childrens of ScreenB
                  ${i},
                  duration: const Duration(milliseconds: 400),
                  curve: Curves.easeInOut,
                );
              },
              child: Container(
                child: Text('Click Me'),
              ),
            )
        ],
      ),
      ),
    );
  }
}

Screen B Code


class _ScreenBState extends State<shlokPage> {

  final PageController _pageController = PageController(); // I need to access this controller in Screen A 


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      body: SafeArea(
        child: PageView(
          controller: _pageController,
          children: [
            for (int i = 0; i < 5; i++) // Both are having same number of loops (Screen A and Screen B)
            Container(
              color: Colors.red,
              child: Center(
                child: Text('Hello World'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

So I'm trying to jump from Screen A to Screen B (to their respective children) with the help of a page controller (pageview widget) So basically, I'm trying to link ListView with the PageView (there are buttons in the ListView that needs to go to PageView children)

shakky
  • 434
  • 5
  • 20
  • I don't fully understand your use case but you might need this https://stackoverflow.com/questions/46057353/controlling-state-from-outside-of-a-statefulwidget/51460832 – developerjamiu Nov 06 '21 at 06:32
  • Sorry if I wasn’t clear enough with my question! Could you please take a look at this question https://stackoverflow.com/questions/69859717/redirect-listview-children-button-to-the-pageview-children @developerjamiu – shakky Nov 06 '21 at 09:44
  • Never mind! I figured it out! – shakky Nov 06 '21 at 14:14
  • @anonymous can you post the solution ? – Salma Jun 10 '22 at 11:20

0 Answers0