Open bahadirarslan opened 2 years ago
Did you find a solution for this ?
Well, I was looking for a solution that I thought it was already integrated in the plugin, but since I couldn't find any, I manage to get this working just by having a callback.
So, lets say you have the following screens:
If in the Dashboard screen you wish to have a button to navigate to Account screen and keep the Navigation bar aligned, here's how you do it:
home_screen.dart
// Defines the initial index of screens
PersistentTabController tabController = PersistentTabController(initialIndex: 0);
// Create your persistentTabView normally
@override
Widget build(BuildContext context) {
return PersistentTabView(
controller: tabController,
screens: screens(),
... etc
);
}
List<Widget> screens() {
return [
DashboardScreen()
onScreenChanged: (int screenIndex) {
tabController.jumpToTab(screenIndex);
},
),
PostsScreen(),
AccountScreen(),
];
}
dashboard_screen.dart
class DashboardScreen extends StatefulWidget {
final void Function(int) onScreenChanged;
const DashboardScreen({
required this.onScreenChanged,
Key? key,
}) : super(key: key);
@override
_DashboardScreenState createState() => _DashboardScreenState();
}
class _DashboardScreenState extends State<DashboardScreen>
{
@override
Widget build(BuildContext context) {
return TextButton(
child: const Text('Go to account screen'),
onPressed: () => widget.onScreenChanged(2),
);
}
}
Hello,
I am using Persistent Bottom Nav Bar in my project and I couldn't achieve a simple thing. While navigating from one screen to another without using the bottom nav bar; I want to update the selected (active) nav bar item according to navigated page.
But I can not make it.
In this example, I try to navigate from HomeScreen to SettingsScreen and I tried all the ways I know. But in the end, I can navigate successfully but the active navigation bar item is at the bottom nav bar still HomeScreen.
or
Navigator.of(context).pushNamed(SettingsScreen.routeName);
or
What is the correct way of doing this?