Here we will see how to open and close drawer from nested menu flutter. You will learn how to do the below
We will see how to achieve the above effect.
class MenuIcon extends StatelessWidget {
const MenuIcon({super.key});
@override
Widget build(BuildContext context) {
final adaptiveColour = Colours.classicAdaptiveTextColour(context);
return Center(
child: GestureDetector(
onTap: () {
DashboardUtils.scaffoldKey.currentState?.openDrawer();
},
child: Iconify(
HeroiconsOutline.menu_alt_2,
size: 24,
color: adaptiveColour,
),
),
);
}
}
Put the above code somewhere in your code, but you need to change the color based on your requirement.
At the same time, you need to keep track of your scaffoldKey in your app.
I did that in a different file call DashboardUtils
abstract class DashboardUtils {
static final scaffoldKey = GlobalKey<ScaffoldState>();
static final iconList = [
(IconlyBroken.home, IconlyBold.home),
(IconlyBroken.discovery, IconlyBold.discovery),
(IconlyBroken.buy, IconlyBold.buy),
(IconlyBroken.heart, IconlyBold.heart),
(IconlyBroken.profile, IconlyBold.profile),
];
static int activeIndex(GoRouterState state) {
return switch (state.fullPath) {
HomeView.path => 0,
ExploreView.path => 1,
CartView.path => 2,
WishlistView.path => 3,
ProfileView.path => 4,
_ => 0,
};
}
}
Here of course scaffoldKey is the most important.
Now we will call MenuIcon() from somewhere else in your code
Here you see we called MenuIcon()