Reputation: 567
I have a show dialog with CircularProgressIndicator that pops up when user press on signin. Once the sign in function finishes, the user will navigate to the next page but the showDialog is staying on the screen. How can I dismiss it when I navigate?
Future googleLogin() async{
showDialog(context: context,
barrierDismissible: false,
builder: (context) => const Center(child: CircularProgressIndicator(),
),
);
try{
final googleUser =
await GoogleSignIn(scopes: ['profile', 'email']).signIn();
if (googleUser == null) return;
_user = googleUser;
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await _auth.signInWithCredential(credential);
}on FirebaseAuthException catch (e){
Utils.showSnackBar(e.message);
}
navigatorKey.currentState!.popUntil((route) => true,);}
I tried using pop until but It seems like Im doing it wrong
Upvotes: 0
Views: 2778
Reputation: 21
Call Navigator.pop(context) to dismiss ShowDialoue
Navigator.push() to move to second page
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(
'I am ShowDialogue',
style: TextStyle(
color:
Color(0xff4E5069).withOpacity(0.9)),
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
Navigator.pushNamed(
context, "secondPage"
);
},
child: Text('Create'
),
),
],
);
},
);
Upvotes: 1
Reputation: 427
Description:- When Dialog box open in your screen and user want to navigate other screen.
Navigator.pop(context);
Future.delayed(const Duration(milliseconds: 500), () { //Add here your next screen navigation code });
I hope solution will helpful for you
Upvotes: 1
Reputation: 448
Put FutureBuilder inside showDialog:
googleLogin() {
showDialog(
context: context,
builder: (context) => FutureBuilder(
future: GoogleSignIn(scopes: ['profile', 'email']).signIn(),
builder: (context, snapshot) {
if (snapshot.hasData) {
login(snapshot.data).then((value) => navigatorKey.currentState!.popUntil((route) => true,));
} else if (snapshot.hasError) {
Utils.showSnackBar(snapshot.error);
navigatorKey.currentState!.popUntil((route) => true,);
}
return const Center(child: CircularProgressIndicator.adaptive());
}
);
);
}
Future login(data) async {
_user = data;
try {
final googleAuth = await data.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await _auth.signInWithCredential(credential);
} catch (err) {
Utils.showSnackBar(err.toString());
}
}
Upvotes: 0