路由信息

void main() {
  runApp(
      new MaterialApp(
        home: new Screen1(),
        routes: <String, WidgetBuilder> {
          '/screen1': (BuildContext context) => new Screen1(),
          '/screen2' : (BuildContext context) => new Screen2(),
          '/screen3' : (BuildContext context) => new Screen3(),
        },
      )
  );
}

pushNamed

Navigator.of(context).pushNamed('routeName');
此种方法只是简单的将我们需要进入的页面push到栈顶,每次都将新建一个新的页面

pushReplacementNamed

Navigator.of(context).pushReplacementNamed('/screen1');
指把当前页面在栈中的位置替换成跳转的页面(关闭当前页面,进入下个页面),当新的页面进入后,之前的页面将执行dispose方法。
下面为官方说明:

Replace the current route of the navigator that most tightly encloses the
given context by pushing the route named [routeName] and then disposing
the previous route once the new route has finished animating in.

使用情况:例如
从SplashScreen到HomeScreen。它应该只显示一次。

pushReplacement

Navigator.pushReplacement( context, MaterialPageRoute(builder: (BuildContext context) => Screen1)));
这个用法跟pushReplacementNamed相同,上方的是传递路由名称,而后者只需new对应页面即可,而且可以传递 参数。

popAndPushNamed

Navigator.popAndPushNamed(context, '/screen41);
指将当前页面pop,然后跳转到制定页面(将当前路由弹出导航器,并将命名路由推到它的位置。)
下面为官方说明:

Pop the current route off the navigator that most tightly encloses the
given context and push a named route in its place.

pushNamedAndRemoveUntil

Navigator.of(context).pushNamedAndRemoveUntil('/screen1', (Route<dynamic> route) => false);
指将制定的页面加入到路由中,然后将其他所有的页面全部pop, (Route route) => false将确保删除推送路线之前的所有路线。 这时候将打开一个新的screen1页面
Navigator.of(context).pushNamedAndRemoveUntil('/screen4', ModalRoute.withName('/screen1'));
指将制定的页面加入到路由中,然后将之前的路径移除知道制定的页面为止(将具有给定名称的路由推到导航器上,然后删除所有路径前面的路由直到’predicate’返回true为止。)
这时候将销毁栈内除了screen1的页面,点击直接去栈内screen1,这时screen1会重新build

pushAndRemoveUntil

Navigator.pushAndRemoveUntil(context,MaterialPageRoute(builder: (BuildContext context) => new Screen4()),ModalRoute.withName('/'),
这种方法跟上述方法作用相同,不同之处在于,上述传递的是路由名称,这个名称需要你在入口处进行路由指定,而这种则无需指定,直接new 出来即可,
而且可以传递参数。
1–>2–>3,3到4时使用此方法,这时候如果在页面4点击返回,将会直接回到页面1。

pop

Navigator.of(context).maybePop();
maybePop 会自动进行判断,如果当前页面pop后,会显示其他页面,不会出现问题,则将执行当前页面的pop操作
否则将不执行。
Navigator.of(context).canPop();
canPop 判断当前页面能否进行pop操作,并返回bool值
Navigator.of(context).pop();
直接退出当前页面`

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐