Flutter界面跳转
定义界面,有点像Android清单文件中申明界面。
可以新建个dart文件,声明
const first = 'first';
const two = 'two';
const three = 'three';
var RoutPath ={
"$first":(context)=> FirstScreen("AAAAA"),
"$two":(context)=> Second(),
"$three":(context)=> ThreeScreen(),
};
再跳转的时候可以这样调用
Navigator.pushNamed(context, 'first');
然后调用返回
Navigator.pop(context);
跳转方法以及含义
//未声明的正常启动
Navigator.push(context, MaterialPageRoute(builder: (context) => Second()));
//销毁所有的界面,并且跳转到first界面
Navigator.pushNamedAndRemoveUntil(context, 'first', (Route<dynamic> route) => false);
// 销毁除了 two 之外的所有界面
Navigator.pushNamedAndRemoveUntil(context, 'two', ModalRoute.withName('two'));
//跳转并替换当前界面
Navigator.pushReplacementNamed(context, "first");
//跳转并销毁当前界面
Navigator.popAndPushNamed(context, "first");
跳转界面并且传值
Navigator.push(context, new MaterialPageRoute(builder: (context)=> FirstScreen("我给你传值A")));
跳转界面获取传入值
String title ;
FirstScreen(String k){
this.title = k;
}
跳转界面,并且获取返回的值
Navigator.pushNamed(context, 'three').then((Object o) {
print('我收到的返回值$o');
});
返回界面,并且传递返回值
Navigator.pop(context, '给你的返回值333');