Skip to content

PageController not displaying the page selected via PlatformNavBar #472

@sunilgummalla

Description

@sunilgummalla

Here is my StatefulWidget code

`class _HomeScreenState extends State {
var currentIndex = 0;
final activeTabColor = Colors.amber;
late final PageController _pageController;

List<_TabInfo> getPages(final BuildContext context) => [
_TabInfo(
title: 'Home',
icon: Icon(context.platformIcons.home),
content: const Center(child: Text('🏠 Home', style: TextStyle(fontSize: 24))),
),
_TabInfo(
title: 'Classifieds',
icon: Icon(context.platformIcons.book),
content: const Center(child: Text('📚 Classifieds', style: TextStyle(fontSize: 24))),
),
_TabInfo(
title: 'Money',
icon: Icon(context.platformIcons.train),
content: const Center(child: Text('💰 Money', style: TextStyle(fontSize: 24))),
),
_TabInfo(
title: 'Shopping',
icon: Icon(context.platformIcons.shoppingCart),
content: const Center(child: Text('🛒 Shopping', style: TextStyle(fontSize: 24))),
),
_TabInfo(
title: 'Profile',
icon: Icon(context.platformIcons.person),
content: const Center(child: Text('👤 Profile', style: TextStyle(fontSize: 24))),
),
_TabInfo(
title: 'Settings',
icon: Icon(context.platformIcons.settings),
content: const Center(child: Text('⚙️ Settings', style: TextStyle(fontSize: 24))),
),
];

List getBottomNavItems(final BuildContext context) {
final listPages = getPages(context);
final listTabItems = [];
for (int i = 0; i < listPages.length; i++) {
listTabItems.add(
BottomNavigationBarItem(
label: listPages[i].title,
icon: listPages[i].icon,
activeIcon: Icon(listPages[i].icon.icon, color: activeTabColor),
),
);
}

return listTabItems;

}

@OverRide
void initState() {
super.initState();
_pageController = PageController(initialPage: currentIndex); // ✅ Initialize once
_pageController.addListener(() {
debugPrint('[PageController] Page changed to ${_pageController.page}');
});
}

@OverRide
Widget build(final BuildContext context) {
return PlatformScaffold(
appBar: PlatformAppBar(
trailingActions: [PlatformIconButton(icon: Icon(context.platformIcons.search), onPressed: () {})],
),
body: SafeArea(
child: PageView(
physics: const BouncingScrollPhysics(),
controller: _pageController,
children: const [
Center(child: Text('🏠 Home', style: TextStyle(fontSize: 24))),
Center(child: Text('📚 Classifieds', style: TextStyle(fontSize: 24))),
Center(child: Text('💰 Money', style: TextStyle(fontSize: 24))),
],
onPageChanged: (final index) {
debugPrint('[body] Page changed to $index');
setState(() {
currentIndex = index; // ✅ Sync currentIndex when swiping (if enabled)
});
},
),
),
bottomNavBar: PlatformNavBar(
currentIndex: currentIndex,
itemChanged: (final index) {
debugPrint('[bottomNavBar] Item changed to $index');
setState(() {
currentIndex = index; // ✅ Update currentIndex
});

_pageController.animateToPage(
  index,
  duration: const Duration(milliseconds: 300), // Smooth transition
  curve: Curves.easeInOut,
);
    },
    items: const [
      BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
      BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
      BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
    ],
  ),
);

}

@OverRide
void dispose() {
_pageController.dispose();
super.dispose();
}
}
`

This code is working fine on Android. but on iOS, I can see that page number is getting updated in the PageController but on UI nothing changes. And from the second click onwards, the application is throwing the below exception:

`══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
The following assertion was thrown while handling a gesture:
ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
Failed assertion: line 172 pos 12: '_positions.length == 1'

Either the assertion indicates an error in the framework itself, or we should provide substantially
more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=2_bug.yml

When the exception was thrown, this was the stack:
#2 ScrollController.position (package:flutter/src/widgets/scroll_controller.dart:172:12)
#3 PageController.animateToPage (package:flutter/src/widgets/page_view.dart:193:41)
#4 _HomeScreenState.build. (package:springboard/screens/home_screen.dart:117:27)
#5 _CupertinoTabScaffoldState.build. (package:flutter/src/cupertino/tab_scaffold.dart:369:40)
#6 CupertinoTabBar._buildTabItems. (package:flutter/src/cupertino/bottom_tab_bar.dart:233:37)
#7 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:357:24)
#8 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:653:11)
#9 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:307:5)
#10 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:240:7)
#11 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:718:9)
#12 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:97:12)
#13 PointerRouter._dispatchEventToRoutes. (package:flutter/src/gestures/pointer_router.dart:143:9)
#14 _LinkedHashMapMixin.forEach (dart:_compact_hash:763:13)
#15 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:141:18)
#16 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:131:7)
#17 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:530:19)
#18 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:499:22)
#19 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:460:11)
#20 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:437:7)
#21 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:394:5)
#22 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:341:7)
#23 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:308:9)
#24 _invoke1 (dart:ui/hooks.dart:332:13)
#25 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:451:7)
#26 _dispatchPointerDataPacket (dart:ui/hooks.dart:267:31)
(elided 2 frames from class _AssertionError)

Handler: "onTap"
Recognizer:
TapGestureRecognizer#9d0cd
════════════════════════════════════════════════════════════════════════════════════════════════════
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions