class NotificationRouteState extends State<NotificationRoute> {
String _msg="";
@override
Widget build(BuildContext context) {
//监听通知
return NotificationListener<MyNotification>(
onNotification: (notification){
print(notification.msg); //打印通知
return false;
},
child: NotificationListener<MyNotification>(
onNotification: (notification) {
setState(() {
_msg+=notification.msg+" ";
});
return false;
},
child: ...//省略重复代码
),
);
}
}
1、上面中两个NotificationListener
进行了嵌套,子NotificationListener
的onNotification
回调返回了false
,表示不阻止冒泡,所以父NotificationListener
仍然会受到通知,所以控制台会打印出通知信息;
2、如果将子NotificationListener
的onNotification
回调的返回值改为true
,则父NotificationListener
便不会再打印通知了,因为子NotificationListener
已经终止通知冒泡了。