taha khamis
taha khamis

Reputation: 567

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

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

Answers (3)

Alina Afzaal
Alina Afzaal

Reputation: 21

  1. Call Navigator.pop(context) to dismiss ShowDialoue

  2. 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

Nitin Gadhiya
Nitin Gadhiya

Reputation: 427

Description:- When Dialog box open in your screen and user want to navigate other screen.

  1. Dialog will be close
  2. you will be navigate new 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

Sulaymon Ne'matov
Sulaymon Ne'matov

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

Related Questions