Flutter的路由

1、定义路由

使用MaterialApp的路由

new MaterialApp(
      routes: {
        //Map<String, WidgetBuilder>
        "/splash": (context) => new SplashPage(),
        "/login": (context) => new LoginPage(),
        "/home": (context) => new HomePage(),
        "/detail": (context) => new DetailPage(),
      },
      onUnknownRoute: (RouteSettings setting) {
        String name = setting.name;
        print("onUnknownRoute:$name");
        return new MaterialPageRoute(builder: (context) {
          return new NotFoundPage();
        });
      },)
自定义路由
PageRouteBuilder
PageRouteBuilder({
    RouteSettings settings,
    @required this.pageBuilder,//构造页面
    this.transitionsBuilder = _defaultTransitionsBuilder,//创建转场动画
    this.transitionDuration = const Duration(milliseconds: 300),//转场动画的持续时间
    this.opaque = true,//是否是透明的
    this.barrierDismissible = false,//举个例子,比如AlertDialog也是利用PageRouteBuilder进行创建的,barrierDismissible若为false,点击对话框周围,对话框不会关闭;若为true,点击对话框周围,对话框自动关闭。
    this.barrierColor,
    this.barrierLabel,
    this.maintainState = true,
  }

2、使用路由

  2.1、push(),

  就是直接将一个元素插入到堆栈的顶部。

Navigator.of(context).pushNamed("/111");
Navigator.of(context).push(route);

2.2、pop(),

就是将堆栈的顶部元素进行删除,回退到上一个界面。

Navigator.of(context).pop();

2.3、删除原顶部页面并在顶部添加一个页面(即替换)

Navigator.of(context).pushReplacementNamed('/screen4');
Navigator.popAndPushNamed(context, '/screen4');
Navigator.of(context).pushReplacement(newRoute);

2.4、删除原来所有页面并在顶部添加一个页面

Navigator.of(context).pushNamedAndRemoveUntil(“/login”,(Route<dynamic> route) => false)
Navigator.of(context).pushAndRemoveUntil(newRoute)

2.5、删除原来部分页面并在顶部添加一个新页面

Navigator.of(context).pushNamedAndRemoveUntil('/screen4',ModalRoute.withName('/screen1'));

2.6、popUntil():单纯的进行移除路由操作

3、路由传参

访问传参:

Navigator.of(context).push(new (){

})

返回传参:

上一个页面返回:

Navigator.of(context).pop("这是返回数据"),
下一个页面接收then():
Navigator.of(context).pushNamed("ListPage").then((value){
String t1=value as String;
})
 

 

posted @ 2019-04-04 15:26  xiaoye2019  阅读(24)  评论(0)    收藏  举报