I would like to use the bottom navigation bar and a drawer for the case that navigation within one tab provides the back button instead of the drawer. So I have 2 tabs of which the first one has a subpage to which I can navigate:
When the user is seeing Tab1 or Tab2 the drawer should be shown and when the user is on Tab1Detail the back button instead of the drawer should be shown. I checked the example code of the project, but even with that code the back button does not appear when navigating to MainScreen2 or MainScreen3.
The preview yet shows on navigation to MainScreen2 that the back button appears, so I'm not sure if I'm missing something or this is not supported (anymore?).
I tried both using Navigator.of(context).pushNamed() as well as the pushNew() / pushNewScreenWithRouteSettings().
Code is from the example but trimmed down.
void main() {
runApp(const ExampleApp());
}
class ExampleApp extends StatelessWidget {
const ExampleApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(title: 'StreamedUp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: TabExample(),
);
}
}
class TabExample extends StatefulWidget {
const TabExample({Key? key}) : super(key: key);
@override
_TabExampleState createState() => _TabExampleState();
}
class _TabExampleState extends State {
late PersistentTabController _controller;
@override
void initState() {
super.initState();
_controller = PersistentTabController(initialIndex: 0);
}
List _buildScreens() {
return [
MainScreen(),
MainScreen(),
];
}
List _navBarsItems() {
return [
PersistentBottomNavBarItem(
icon: Icon(Icons.home),
title: "Home",
activeColorPrimary: Colors.blue,
inactiveColorPrimary: Colors.grey,
inactiveColorSecondary: Colors.purple,
routeAndNavigatorSettings: RouteAndNavigatorSettings(
initialRoute: '/',
routes: {
'/first': (context) => MainScreen2(),
},
),
),
PersistentBottomNavBarItem(
icon: Icon(Icons.search),
title: ("Search"),
activeColorPrimary: Colors.teal,
inactiveColorPrimary: Colors.grey,
),
];
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Navigation Bar Demo')),
drawer: Drawer(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('This is the Drawer'),
],
),
),
),
body: PersistentTabView(
context,
controller: _controller,
screens: _buildScreens(),
items: _navBarsItems(),
hideNavigationBar: false,
popAllScreensOnTapOfSelectedTab: true,
itemAnimationProperties: ItemAnimationProperties(
duration: Duration(milliseconds: 400),
curve: Curves.ease,
),
screenTransitionAnimation: ScreenTransitionAnimation(
animateTabTransition: true,
curve: Curves.ease,
duration: Duration(milliseconds: 200),
),
navBarStyle:
NavBarStyle.style13, // Choose the nav bar style with this property
),
);
}
}
class MainScreen extends StatelessWidget {
const MainScreen({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.indigo,
body: Center(
child: ElevatedButton(
onPressed: () {
pushNewScreenWithRouteSettings(
context,
settings: RouteSettings(name: '/home'),
screen: MainScreen2(),
pageTransitionAnimation: PageTransitionAnimation.scaleRotate,
);
//Alternatively tried: Navigator.pushNamed(context, '/first'), but also not working
},
child: Text(
"Go to Second Screen ->",
style: TextStyle(color: Colors.white),
),
),
),
);
}
}
class MainScreen2 extends StatelessWidget {
const MainScreen2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.teal,
body: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(
"Go Back to First Screen",
style: TextStyle(color: Colors.white),
),
),
],
),
),
),
);
}
}