With the current code, on click of back button, the app exits instead of moving to the previous page.
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
class homeNav extends StatefulWidget {
const homeNav({super.key});
@override
State<homeNav> createState() => _homeNavState();
}
class _homeNavState extends State<homeNav> {
int currindex = 2;
final screens = const [
cart(),
HomeScreen(),
HomeScreen(),
searchPage(),
MyProfile(),
];
@override
Widget build(BuildContext context) {
const items = <Widget>[
Icon(Icons.shopping_cart,size: 30,color: Colors.white,),
Icon(Icons.favorite,size: 30,color: Colors.white,),
Icon(Icons.home,size: 30,color: Color.fromARGB(255, 255, 255, 255),),
Icon(Icons.search,size: 30, color: Colors.white,),
Icon(Icons.person,size: 30,color: Colors.white,),
];
return Scaffold(
extendBody: true,
backgroundColor: Colors.transparent,
bottomNavigationBar: currindex == 0
? null
: CurvedNavigationBar(
color: Color.fromARGB(234, 7, 123, 255),
buttonBackgroundColor: Color.fromARGB(255, 11, 43, 99),
backgroundColor: Colors.transparent,
index: currindex,
height: 60,
items: items,
onTap: (index) => setState(() => this.currindex = index),
),
body: screens[currindex],
);
}
}
Dependency used: curved_navigation_bar
I have marked the bottomNavigationBar
to be invisible for currindex==1
(Cart()) page.
I want the app to redirect to previous page on tap of back button.
Tried this answer from How to use BottomNavigationBar with Navigator?
But didnt work
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
class homeNav extends StatefulWidget {
const homeNav({super.key});
@override
State<homeNav> createState() => _homeNavState();
}
class _homeNavState extends State<homeNav> {
int currindex = 2;
final screens = const [
cart(),
HomeScreen(),
HomeScreen(),
searchPage(),
MyProfile(),
];
final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
@override
Widget build(BuildContext context) {
const items = <Widget>[
Icon(
Icons.shopping_cart,
size: 30,
color: Colors.white,
),
Icon(
Icons.favorite,
size: 30,
color: Colors.white,
),
Icon(
Icons.home,
size: 30,
color: Color.fromARGB(255, 255, 255, 255),
),
Icon(
Icons.search,
size: 30,
color: Colors.white,
),
Icon(
Icons.person,
size: 30,
color: Colors.white,
),
];
return SafeArea(
child: Scaffold(
extendBody: true,
backgroundColor: Colors.transparent,
bottomNavigationBar: currindex == 0
? null
: CurvedNavigationBar(
color: Color.fromARGB(234, 7, 123, 255),
buttonBackgroundColor: Color.fromARGB(255, 11, 43, 99),
backgroundColor: Colors.transparent,
index: currindex,
height: 60,
items: items,
// onTap: (index) => setState(() => this.currindex = index),
onTap: _onTap,
),
body: Navigator(key: _navigatorKey, onGenerateRoute: generateRoute),
// bottomNavigationBar: _bottomNavigationBar(),
),
);
}
_onTap(int tabIndex) {
switch (tabIndex) {
case 0:
_navigatorKey.currentState?.pushReplacementNamed("cart");
break;
case 1:
_navigatorKey.currentState?.pushReplacementNamed("home");
break;
case 2:
_navigatorKey.currentState?.pushReplacementNamed("home");
break;
case 3:
_navigatorKey.currentState?.pushReplacementNamed("search");
break;
case 4:
_navigatorKey.currentState?.pushReplacementNamed("profile");
break;
}
setState(
() {
currindex = tabIndex;
},
);
}
Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case "home":
return MaterialPageRoute(builder: (context) => HomeScreen());
case "cart":
return MaterialPageRoute(builder: (context) => cart());
case "search":
return MaterialPageRoute(builder: (context) => searchPage());
case "profile":
return MaterialPageRoute(builder: (context) => MyProfile());
default:
return MaterialPageRoute(builder: (context) => HomeScreen());
}
}
}