0

I've got;

  • screen/widget Home() which calls;

  • screen/widget MainStage() which calls;

  • future method futureStage() which builds;

  • PageViewBuilder StageBuilder() which contains;

SwipeGestureRecognizer which calls;

Navigator.push (context,
                  PageTransition(
                    type: PageTransitionType.downToUp,
                    child: HomeReply(),
                  ));
  • HomeReply() contains;
  • appBar with an arrow/button that allows the user to;
  • Navigator.pop(context);

How do I get the Navigator to pop back to Home()?

Meggy
  • 1,491
  • 3
  • 28
  • 63

2 Answers2

1

The navigator is a stack so you can use the popUntil method to pop back to your home() screen.

Navigator.popUntil(context, ModalRoute.withName('/home'));

or

Navigator.of(context).popUntil((route) => route.settings.name == "Home");
Zeeshan Hussain
  • 802
  • 6
  • 9
  • Sorry, I didn't saw your comment while writing my answer. Will delete it. – Zeeshan Hussain Aug 03 '20 at 11:36
  • I'm guessing that didn't work because I didn't use a named route in the first place. I've been using a BottomNavigationBar in Home() and it seemed easier to just call Navigator.push (context, PageTransition( type: PageTransitionType.downToUp, child: HomeReply(), )); – Meggy Aug 03 '20 at 12:29
  • So is there a way I can call Home() with popUntil? – Meggy Aug 03 '20 at 12:33
  • You can use route.settings.name. I personally did not try it without named parameters but I guess you can dehug a bit to see what routename is saved when you push home () and then use that? – Zeeshan Hussain Aug 03 '20 at 12:35
  • 1
    I used this code to check my Home() route. var route = ModalRoute.of(context); if(route!=null){ print(route.settings.name); } It gave me "/". So then I tried this; Navigator.of(context).popUntil((route) => route.settings.name == "/"); But unfortunately it didn't work. – Meggy Aug 03 '20 at 20:45
1

Turns out HomeReply() had an extra MaterialApp in the build. Once I removed this it all worked as normal with Navigator.pop(context);.

Meggy
  • 1,491
  • 3
  • 28
  • 63