简体   繁体   中英

flutter navigation pop always pop to root

I'm new on flutter and I got a problem with navigation pop system. I don't understand what's wrong on my code but what should I check on my code if every time I pressed back button on phone(not on AppBar or anything else), it goes back to the root of my page, not the previous page. but it works perfectly when I press back using the app bar back button that created automatically.

I've tried to fix this issue by passing some unpassed BuildContext to my separated widget hoping that's the problem but still doesn't works

Example: I have three pages A, B, C

A->B->C

A->B->D

A->B->E

when I press smartphone's back button on B it goes to A (works).

when I press Scaffold appbar back button on C it goes to B (still works),

But, when I press smartphone's back button on C, D, or E it goes to A (not works)

//this is where I initialize my route
class _LoginPageState extends State<LoginPage>{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: <String, WidgetBuilder> {
      '/main': (BuildContext context) => MainPage(),
      // '/b': (BuildContext context) => MyPage(title: 'page B'),
      // '/c': (BuildContext context) => MyPage(title: 'page C'),
    },
.....


// and this is how I push 
onPressed: () {
  Navigator.of(context).pushNamed('/main');
}

Thanks to @HassanSaleh giving me a little clue about the material app. I have misunderstood about using material app route. I initialize material app route on every page and I read carefully on flutter dev cookbook about navigating page and found out that I should just put materialapp only on the very top of my widget stack. So the fix for me is Removed material app on every widget and only put one material app and its route on very root app Widget . Maybe I have bad explanation hope u guys understand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM