);
}
}
B页面代码
class BPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.blue),
home: Scaffold(
appBar: AppBar(title: Text(“BPage”),),
body: Container(
alignment: Alignment.center,
child: RaisedButton(
chil
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
d: Text(“B页面”),
onPressed: () {
Navigator.of(context).pop();
},
),
),
),
);
}
}
3.4 效果图
3.5 堆栈变化
3.5.1 M页面跳转到B页面时
当应用程序位于M页面时,路由堆栈中只有M,点击按钮跳转到B页面,路由堆栈中有 B 和M,且 B 处于栈顶。
3.5.2 B页面返回M页面时(pop)
位于栈顶的B出栈,堆栈中只有M
3.5.3 从 B 页面跳转到 M 页面,使用push方法
RaisedButton(
child: Text(‘B 页面’),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return MyApp();
}));
},
)
路由堆栈
3.5.4 栈顶返回(pop/maybePop/canPop)
在M 页面时路由堆栈中只有 M,调用 pop 后,堆栈中已经没有任何内容,此时路由堆栈为空,没有可显示的页面,应用程序将会退出或者黑屏
解决办法:也可以通过 canPop 判断当前是否可以 pop
if(Navigator.of(context).canPop()){
Navigator.of(context).pop();
}
4.1 在MaterialApp中配置路由名称
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: <String, WidgetBuilder>{
“/M”:(context)=>MyApp(),
“/B”:(context)=>BPage(),
},
title: ‘Flutter Demo’,
theme: ThemeData(primarySwatch: Colors.blue,),
home: MyHomePage(title: ‘Flutter Demo Home Page’),
);
}
}
4.2 页面间跳转
4.2.1 pushNamed方式(M跳转B)
标准跳转方式,每次跳转都将页面假如到堆栈中
RaisedButton(
child: Text(‘M 页面’),
onPressed: () {
Navigator.of(context).pushNamed(’/B’);
},
)
4.2.2 pushReplacementNamed 方式
将要添加的页面堆栈替换当前堆栈
有A、B、C 三个页面,A页面通过 pushNamed 跳转到 B:
RaisedButton(
child: Text(‘M 页面’),
onPressed: () {
Navigator.of(context).pushNamed(’/B’);
},
)
通过 pushReplacementNamed 跳转到 C:
RaisedButton(
child: Text(‘B 页面’),
onPressed: () {
Navigator.of(context).pushReplacementNamed(’/C’);
},
)
点击 C 页面按钮执行 pop:
RaisedButton(
child: Text(‘C 页面’),
onPressed: () {
if(Navigator.of(context).canPop()){
Navigator.of(context).pop();
}
},
)
效果图
堆栈变化
点击 C 页面按钮直接返回到了 A 页面,而不是 B 页面,因为 B 页面使用 pushReplacementNamed 跳转,路由堆栈变化
4.2.3 popAndPushNamed
B 页面跳转到 C 页面,使用 popAndPushNamed
RaisedButton(
child: Text(‘B 页面’),
onPressed: () {
Navigator.of(context).popAndPushNamed(’/C’);
},
)
popAndPushNamed和pushReplacementNamed的区别
-
popAndPushNamed 路由堆栈和 pushReplacementNamed 是一样,唯一的区别就是
-
popAndPushNamed 有 B 页面退出动画
4.2.4 pushNamedAndRemoveUntil
应用场景
有如下场景,应用程序进入首页,点击登录进入登录页面,然后进入注册页面或者忘记密码页面…,登录成功后进入其他页面,此时不希望返回到登录相关页面,此场景可以使用
有A、B、C、D 四个页面,A 通过push进入 B 页面,B 通过push进入 C 页面,C 通过 pushNamedAndRemoveUntil 进入 D 页面同时删除路由堆栈中直到 /B 的路由,C 页面代码
RaisedButton(
child: Text(‘C 页面’),
onPressed: () {
Navigator.of(context).pushNamedAndRemoveUntil(’/D’, ModalRoute.withName(’/B’));
},
),
D 页面按钮执行 pop
RaisedButton(
child: Text(‘D 页面’),
onPressed: () {
Navigator.of(context).pop();
},
)
效果图
从 C 页面跳转到 D 页面路由堆栈变化
4.2.5 popUntil
有A、B、C、D 四个页面,D 页面通过 popUntil 一直返回到 A 页面,D 页面代码:
RaisedButton(
child: Text(‘D 页面’),
onPressed: () {
Navigator.of(context).popUntil(ModalRoute.withName(’/A’));
},
)
效果图
路由堆栈变化
5.1 通过构造函数方式
商品详情页
class ProductDetail extends StatelessWidget {
final ProductInfo productInfo;
const ProductDetail({Key key, this.productInfo}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.blue),
home: Scaffold(
body: Container(
alignment: Alignment.center,
child: Text(“ProductDetail:${productInfo.name}”),
),
),
);
}
}
要传递的值
class ProductInfo {
var name;
ProductInfo(this.name);
}
跳转代码
Navigator.of(context).push(MaterialPageRoute(builder: (context){
return ProductDetail(productInfo: ProductInfo(“张三”),);
}));
效果图
5.2 通过命名路由设置参数的方式
A 页面传递数据
RaisedButton(
child: Text(‘A 页面’),
onPressed: () {
Navigator.of(context).pushNamed(’/B’,arguments: ‘来自A’);
},
)
B 页面通过 ModalRoute.of(context).settings.arguments
接收数据:
RaisedButton(
child: Text(’${ModalRoute.of(context).settings.arguments}’),
onPressed: () {
(this.name);
}
跳转代码
Navigator.of(context).push(MaterialPageRoute(builder: (context){
return ProductDetail(productInfo: ProductInfo(“张三”),);
}));
效果图
[外链图片转存中…(img-DMHt4Wp5-1638555526517)]
5.2 通过命名路由设置参数的方式
A 页面传递数据
RaisedButton(
child: Text(‘A 页面’),
onPressed: () {
Navigator.of(context).pushNamed(’/B’,arguments: ‘来自A’);
},
)
B 页面通过 ModalRoute.of(context).settings.arguments
接收数据:
RaisedButton(
child: Text(’${ModalRoute.of(context).settings.arguments}’),
onPressed: () {