父组件跳转子组件
- Navigator.push 是跳转到下一个页面,它要接受两个参数一个是context(上下文),另一个是要跳转的函数。
- Navigator.pop 是返回到上一个页面,使用时传递一个context(上下文)参数,使用时要注意的是,你必须是有上级页面的,也就是说上级页面使用了Navigator.push ```dart import ‘package:flutter/material.dart’;
//传递的数据结构,也可以理解为对商品数据的抽象 class Product { final String title; //商品标题 final String description; //商品描述 Product(this.title, this.description); }
// void main() { // runApp(MaterialApp( // title: ‘数据传递案例’, // // List.generate生成的。并且这个生成的List原型就是我们刚开始定义的Product这个类(抽象数据) // home: ProductList( // products: List.generate(20, (i) => Product(‘商品 $i’, ‘这是一个商品详情,编号为:$i’)), // ))); // }
//分离后的 void main() => runApp(MyApp()); class MyApp extends StatelessWidget { const MyApp({Key key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: ‘数据传递案例’, // List.generate生成的。并且这个生成的List原型就是我们刚开始定义的Product这个类(抽象数据) home: ProductList( products: List.generate(20, (i) => Product(‘商品 $i’, ‘这是一个商品详情,编号为:$i’)), )); } }
class ProductList extends StatelessWidget {
final List
class ProductDetail extends StatelessWidget { //接收父组件传来的参数 final Product product; ProductDetail({Key key, @required this.product}) : super(key: key);
@override Widget build(BuildContext context) { return new Scaffold( appBar: AppBar( title: Text(‘${product.title}’), ), body: Center( child: Text(‘${product.description}’), )); } }
<a name="KMfF7"></a>
### 导航参数的传递和接收
```dart
import 'package:flutter/material.dart';
//传递的数据结构,也可以理解为对商品数据的抽象
class Product {
final String title; //商品标题
final String description; //商品描述
Product(this.title, this.description);
}
void main() {
runApp(MaterialApp(
title: '数据传递案例',
home: ProductList(
products: List.generate(20, (i) => Product('商品 $i', '这是一个商品详情,编号为:$i')),
)));
}
class ProductList extends StatelessWidget {
final List<Product> products;
ProductList({Key key, @required this.products}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('商品列表')),
body: ListView.builder(
itemCount: products.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(products[index].title),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
new ProductDetail(product: products[index])));
});
},
));
}
}
class ProductDetail extends StatelessWidget {
final Product product;
ProductDetail({Key key, @required this.product}) : super(key: key);
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text('${product.title}'),
),
body: Center(
child: Text('${product.description}'),
));
}
}
页面跳转并返回数据
import 'package:flutter/material.dart';
void main(){
runApp(MaterialApp(
title:'页面跳转返回数据',
home:FirstPage()
));
}
class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar:AppBar(title:Text("找小姐姐要电话")),
body:Center(
child: RouteButton(),
)
);
}
}
//跳转的Button
class RouteButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed:(){
_navigateToXiaoJieJie(context);
},
child: Text('去找小姐姐'),
);
}
_navigateToXiaoJieJie(BuildContext context) async{ //async是启用异步方法
final result = await Navigator.push(//等待
context,
MaterialPageRoute(builder: (context)=> XiaoJieJie())
);
Scaffold.of(context).showSnackBar(SnackBar(content:Text('$result')));
}
}
class XiaoJieJie extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar:AppBar(
title:Text('我是小姐姐')
),
body:Center(
child:Column(
children: <Widget>[
RaisedButton(
child: Text('大长腿小姐姐'),
onPressed: (){
Navigator.pop(context,'大长腿:1511008888');
},
) ,
RaisedButton(
child: Text('小蛮腰小姐姐'),
onPressed: (){
Navigator.pop(context,'小蛮腰:1511009999');
},
) ,
],
)
) ,
);
}
}
SnackBar的使用
SnackBar是用户操作后,显示提示信息的一个控件,类似Tost
Scaffold.of(context).showSnackBar(SnackBar(content:Text('$result')));
返回数据的方式
返回数据其实是特别容易的,只要在返回时带第二个参数就可以了。
Navigator.pop(context,'xxxx'); //xxx就是返回的参数
Navigator.pop()的源代码
@optionalTypeArgs
static void pop<T extends Object?>(BuildContext context, [ T? result ]) {
Navigator.of(context).pop<T>(result);
}
Flutter 返回上一页并刷新
用flutter路由跳转页面时,主要用到的就是 Navigator.push() 和 Navigator.pop() 两个方法。
但是存在一个问题:
当我从主页跳转到另一个页面,再返回到主页时,主页并不能主动刷新。怎么解决呢?
答案时,当返回到主页时,监听到返回事件,然后主动触发主页刷新。
class PageOne extends StatefulWidget {
@override
_PageOneState createState() => new _PageOneState();
}
class _PageOneState extends State<PageOne> { // 第一个页面
_getRequests()async{
print('这里进行操作');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(onPressed: ()=>
Navigator.of(context).push(new MaterialPageRoute(builder: (_)=>PageTwo()),)
.then((val)=>val?_getRequests():null), // 通过then进行监听回调参数
),
));
}
}
class PageTwo extends StatelessWidget { // 第二个页面
@override
Widget build(BuildContext context) {
//somewhere
Navigator.pop(context,true); // 第二个就是需要传到参数
}
}