flutter实现页面跳转的两种路由

1.无名路由push,直接使用
void nextPage() async{   final result = await Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) {     
return NextPage("pass params to next page");//构造函数传递参数   }));
  //result页面返回结果
}
Navigator.pop(context, "return value to last page");//页面返回

2. 命名路由pushNamed,先注册再使用
void main() => runApp(MyApp());//单行函数调用写法
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "My App Title",
theme: ThemeData(primaryColor: Colors.green),
//home: RandomWord(),首页设置方式一
initialRoute: "home_route",//首页设置方式二
routes: {//路由注册表
"next_page": (BuildContext context) {
return NextPage(ModalRoute.of(context).settings.arguments);
},
"home_route": (BuildContext context) => RandomWord(),
},
// onGenerateRoute: //页面拦截处理
// onUnknownRoute: //页面跳转出错
);
}
}

void nextPage() async{
final result = await Navigator.pushNamed(context, "next_page",arguments: "pass params to next page");
//result页面返回结果
}

//next_page获取上一个页面传递过来的参数的方法:ModalRoute.of(context).settings.arguments


 

posted @ 2019-12-28 16:55  yongfengnice  阅读(3563)  评论(0编辑  收藏  举报