Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to dismiss the sheet manually if there is navigation inside? #49

Closed
davis33321 opened this issue Jul 17, 2020 · 3 comments
Closed

Comments

@davis33321
Copy link

Understand that normally we use Navigator.of(context).pop(); to dismiss the modal sheet.

However in my case, there is navigation inside the modal sheet. So, using Navigator.of(context).pop(); will only return to the previous page of the sheet.

I tried to google and use methods such as Navigator.of(context).popUntil((route) => route.isFirst); but still fail.

Anyone can shed some light on this? Many thanks.

@jamesblasco
Copy link
Owner

jamesblasco commented Jul 17, 2020

For solving this you need to understand how BuildContext works.

Navigator.of(context) will get you the closest Navigator to the context reference. As you have added a Navigator, it will return that Navigator and not the one you used to push the modal button sheet.

For getting the Navigator you used for pushing the modal, you have different options.

The preferred is to use a key to reference the navigator, you have a great example this:
https://stackoverflow.com/a/53397266/8096916

Otherwise, you can pass a context reference from before the inner new Navigator

showBarModalBottomSheet(
                              expand: true,
                              context: context,
                              backgroundColor: Colors.transparent,
                              builder: (context, scrollController) {
return Navigator(
          onGenerateRoute: (_) => MaterialPageRoute(
            builder: (context2) => Builder(
              builder: (context3) => CupertinoPageScaffold(
                ...
                                children: <Widget>[
                                  GestureDetector(
                                    child: Container(
                                        ...
                                    onTap: () {
                                      // context2 or context3 will return the Navigator inside the modal
                                      Navigator.pop(context);
                                    },
                                  ),


                                 }
                                
                            ),
);

@jamesblasco
Copy link
Owner

As this is not an issue related to the library, I am going to close the issue, feel free to ask more if you don't manage to solve it.

@davis33321
Copy link
Author

Thanks a lot for your timely help, @jamesblasco. Lifesaver!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants