Flutter组件之跳转
##页面跳转:
打开并跳转到子页面,点击返回或者左上角箭头都会返回到父页面(并未消失),常用来展示物品详情
例子
// 跳转
RaisedButton(
child: Text("查看商品详情页"),
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => new SecondScreen()));
},
)
// 返回
RaisedButton(
child: Text("返回列表"),
onPressed: () {
Navigator.pop(context);
}
)
##带参跳转:
传值给某一组件
例子
1. 在使用的地方调用,括号内添加参数数据
new SecondScreen(data: "可以是一个对象");
2. 在组件内需要定义接收
class SecondScreen extends StatelessWidget {
//定义接收数据
final String data;
//定义构造函数,继承super,@required为需求参数
SecondScreen({Key key, @required this.data}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("详情页"),
),
body: Center(
child: RaisedButton(
child: Text("信息: $data"),
onPressed: () {
...
}),
),
);
}
}