1
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        children: _children,
        controller: pageController,
        onPageChanged: onPageChanged,
      ),
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTabTapped,
        backgroundColor: Colors.grey[100],
        currentIndex: _currentIndex,
        items: [
          BottomNavigationBarItem(
            icon: new Icon(Icons.navigation),
            label: 'Track',
          ),
          BottomNavigationBarItem(
            icon: new Icon(Icons.settings),
            label: 'Settings',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: 'Profile',
          )
        ],
      ),
    );
  }

  void onTabTapped(int value) {
    pageController.jumpToPage(value);
  }

  void onPageChanged(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

Hi, I've attached my build method of my stateful widget of my simple flutter app in the code image below. I am using a bottom navigation bar and want my widgets to persist in the tree when switching between the bottom pages. Thus, I implemented AutomaticKeepAliveMixin along with the page controller and page body to make the states persist. now my problem is,

  1. my first page is a google maps widget that shows tracking information. Due to pageview, when I try to look at a different location on map by swiping in the direction of the destination, sometimes the right to left swipe on map causes page view to trigger page switch
  2. If I remove pageview to make the map work as expected, the persisting of states is gone because as per the documentation, I should have the body of my scaffold wrapped in a PageView widget for the AutomaticKeepAliveMixin work.

My app screenshot

Now my question is how do I achieve something that does both? I am stuck. I've tried to implement other state persisting techniques like indexed stack and pagekeystorage but no luck :( Any help would be appreciated! Thank you!

Vertical scrolling works but when I try to scroll sideways in my map, the pageview takes precendence and just scrolls to new page like this: Issue GIF

  • Don't post code as picture, use code formatting please. This makes it easier to test your code. just paste it into a block of \`\`\`dart \`\`\` – Maritn Ge Dec 25 '20 at 15:05
  • 1
    I got your point. I did make it a code block now. I've also added a gif to exactly convey my issue. – Akash Deodhar Dec 25 '20 at 15:16
  • Perfect! Hope it gets solved soon. – Maritn Ge Dec 25 '20 at 15:18
  • Looks like a duplicate of https://stackoverflow.com/questions/49439047/how-to-preserve-widget-states-in-flutter-when-navigating-using-bottomnavigation – spkersten Dec 25 '20 at 16:23
  • @spkersten No sir, that's not my issue, I can already persist the state without issues. My issue is how to work with google map as a widget embedded inside the pageview as widget. – Akash Deodhar Dec 25 '20 at 23:11

1 Answers1

1

PageView has a physics property that you can use to control how it scrolls in response to user swiping on it. If you assign NeverScrollableScrollPhysics(), it won’t be scrollable.

spkersten
  • 2,849
  • 21
  • 20