flutter_platform_widgets
flutter_platform_widgets copied to clipboard
PageController not displaying the page selected via PlatformNavBar
Here is my StatefulWidget code
`class _HomeScreenState extends State<HomeScreen> { 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<BottomNavigationBarItem> getBottomNavItems(final BuildContext context) { final listPages = getPages(context); final listTabItems = <BottomNavigationBarItem>[]; 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.
Handler: "onTap" Recognizer: TapGestureRecognizer#9d0cd ════════════════════════════════════════════════════════════════════════════════════════════════════ `
Can you please provide minimum runnable code for me to reproduce the bug?