In side drawer created by me, when user clicks on any link, user goes to that screen and drawer closes as well, no issues with that, also when user opens side drawer and press back key, drawer closes, this is also working.
However, suppose user clicked on 'My Profile' link (or any link), and if then user presses back key from the profile screen to go back to home screen (or uses back key in appbar), user finds side drawer opened on home screen. Then either user has to press back key or tap anywhere on the screen to close drawer.
Initially, my drawer was not closing on back-press even, I found this post [https://stackoverflow.com/q/60314156] and implemented WillPopScope() [Actually that helped me to close drawer on backpress], however the issue which I have explained, is still unresolved with WillPopScope.
Kindly let me know what changes I have to do to implement this behavior.
This is how I have created files : HomePage which has BottomNavigationDrawer through which user comes to shopping page (which is labeled as 'Home' actually), and this is the page where user see side drawer. So above while explaining when I said home, I meant Shopping Page from where user navigated to profile page.
class ShoppingPage extends StatefulWidget {
@override
_ShoppingPageState createState() => _ShoppingPageState();
}
class _ShoppingPageState extends State<ShoppingPage> {
GlobalKey<ScaffoldState> _key = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
ThemeData getTheme = Theme.of(context);
return WillPopScope(
onWillPop: () async {
if (_key.currentState.isDrawerOpen) {
Navigator.of(context).pop();
return false;
}
return true;
},
child: Scaffold(
key: _key,
appBar: AppBar(
title: ,
elevation: 0,
),
drawer: CustomDrawer(),
body: SafeArea(
child: Container(
you simply call :-
Navigator.pop(context);
before navigating to another screen from drawer. Like when user clicks on your profile then in onTap method first pop it then navigate.
onTap:(){
Navigator.pop(context);
//then write navigation statement over here
}