I have simple drawer and I added this to my mainpage's Scaffold
with drawer: CustomizedDrawer()
line, and the drawer has only 1 ListTile to Login or Log Out the user. In my main page, you can open & close the drawer from left to right.
I'm using Firebase Authentication.
If there is already logged-in user, I can see Log Out text. If I click it, I call logOutUser
method in my AuthService
class. Here you can see my drawer:
class CustomizedDrawer extends StatelessWidget
{
@override
Widget build(BuildContext context) {
return Drawer(
backgroundColor: Colors.white,
child: ListView(
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.green,
),
child: Image(
image: AssetImage("images/myimage.png"),
alignment: Alignment.center,
),
),
checkUserIsLoggedIn(context),
],
),
);
}
ListTile checkUserIsLoggedIn(context){
if(FirebaseAuth.instance.currentUser == null) {
return ListTile(
title: const Text('Login / Sign Up'),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => LoginScreen()));
},
);
}
else {
return ListTile(
title: const Text('Log out'),
onTap: () async {
await showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context)
{
return ProgressDialog(message: "Please wait...",);
}
);
AuthService authService = AuthService();
authService.logOutUser(context);
},
);
}
}
}
Here you can get my logOutUser
method in my AuthService
class:
void logOutUser(context) async{
try {
await FirebaseAuth.instance.signOut()
.catchError((errMsg){
Navigator.pop(context);
displayToastMessage("Error: " + errMsg.toString(), context);
});
//Navigator.of(context).push(MaterialPageRoute(builder: (context) => LoginScreen()));
}
catch (e){
showDialog(
context: context,
builder: (context)
{
return AlertDialog(
title: Text("Error Message"),
content: Text(e.toString()),
);
}
);
Navigator.pop(context);
}
}
What I want is; when I click Log Out, I want to see my ProgressDialog
,then log out the user, then close the dialog automatically, then close drawer automatically, then I can see my mainpage.
Regarding an answer here , I tried it. I click Log Out, it shows dialog, it log outs, but dialog is not closing and I can't close drawer.
What am I missing? I guess I'm doing something wrong with await
and async
words, but I need help...