Home > Mobile >  How to dismiss a showDialog when I navigate to the next page in flutter?
How to dismiss a showDialog when I navigate to the next page in flutter?

Time:07-08

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

CodePudding user response:

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());
  }
}

CodePudding user response:

Navigator.pop(context);

Future.delayed(const Duration(milliseconds: 500), () { //Add here your next screen navigation code });

I hope solution will helpful for you

  • Related