颤振导航

Flutter navigation

有人能解释一下为什么从 pageE 返回时不打印 efeioi 吗?

A页

Navigator.pushNamed(context, PageB.ROUTE).then((onValue) {
               print("efeioi");
              });

B页

  Navigator.of(context)
              .pushReplacementNamed(PageC.ROUTE, arguments: onValue);

C页

 Navigator.pushNamed(context, PageD.ROUTE,
                                        arguments: onValue);

PageD

    Navigator.pop(context);  // back to Page C

C页

   Navigator.pushNamed(context, PageE.ROUTE,
                                        arguments: onValue);

E页

 Navigator.of(context).popUntil(ModalRoute.withName(PageA.ROUTE));

我不能在页面 E 中使用 Navigator.pop,因为它会返回到页面 C!

我已经在这里上传了完整的代码

https://github.com/tony123S/navigation

根据您的要求,我已经实现如下 main.dart

initState : this will be called when you navigate from E to A
refreshPage :  it will not called as you already popped before returning to A Page

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
return MaterialApp(
  title: 'Flutter Demo',
  theme: ThemeData(
    primarySwatch: Colors.blue,
  ),
  home: A(),
  routes: <String, WidgetBuilder>{
    '/A': (BuildContext context) => new A(),
    '/B': (BuildContext context) => new B(),
    '/C': (BuildContext context) => new C(),
    '/D': (BuildContext context) => new D(),
    '/E': (BuildContext context) => new E(),
  },
);
  }
}

class A extends StatefulWidget {
  @override
  _FirstRouteState createState() => _FirstRouteState();
}

class _FirstRouteState extends State<A> {
  final String fromPage;

  _FirstRouteState({Key key, @required this.fromPage});

  @override
  void initState() {
// TODO: implement initState
super.initState();
  print("Called askdfjaksdfj");
  }

  @override
  Widget build(BuildContext context) {

return Scaffold(
  appBar: AppBar(
    title: Text('Page A'),
  ),
  body: Center(
    child: RaisedButton(
      child: Text('Open B'),
      onPressed: () {
        // Navigate to second route when tapped.
//            Navigator.push(
//              context,
//              MaterialPageRoute(builder: (context) => B()),
//            );

        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => B()),
        ).then((res) => refreshPage());
      },
    ),
  ),
);
  }

  refreshPage() {
print("refresh page is called");
  }
}

class B extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("B Page"),
  ),
  body: Center(
    child: RaisedButton(
      onPressed: () {
        // Navigate back to first route when tapped.
        Navigator.of(context).pushNamed(
          "/C",
        );
      },
      child: Text('Go to C'),
    ),
  ),
);
  }
}

class C extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("C Page"),
  ),
  body: Center(
    child: Column(
      children: <Widget>[
        RaisedButton(
          onPressed: () {
            // Navigate back to first route when tapped.
            Navigator.pushNamed(
              context,
              "/D",
            );
          },
          child: Text('Go to D'),
        ),
        RaisedButton(
          onPressed: () {
            // Navigate back to first route when tapped.
            Navigator.pushNamed(
              context,
              "/E",
            );
          },
          child: Text('Go to E'),
        ),
      ],
    ),
  ),
);
  }
}

class D extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("D Page"),
  ),
  body: Center(
    child: RaisedButton(
      onPressed: () {
        // Navigate back to first route when tapped.
        Navigator.pop(context);
      },
      child: Text('Go back to C'),
    ),
  ),
);
  }
}

class E extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("E Page"),
  ),
  body: Center(
    child: RaisedButton(
      onPressed: () {
//            Navigator.pop(context);
//            Navigator.of(context).pushNamed("/A");
//            Navigator.of(context).popUntil(ModalRoute.withName('/A'));
        Navigator.of(context)
            .pushNamedAndRemoveUntil('/A', (Route<dynamic> route) => false,);

      },
      child: Text('Go to A'),
    ),
  ),
);
  }
}

请运行代码以便更好地理解,如果您发现任何困难,请回复