《单页面使用》
在需要使用的页面来用。
比如说在,评论列表中更新数据,则我们就可以使用这样的方法来,在当页面操作数据不能共享。
import 'package:YuWeiFlutterApp/src/customNotification.dart';import 'package:YuWeiFlutterApp/src/userbean.dart';import 'package:flutter/material.dart';class IndexInHeritedWidget extends StatefulWidget {@override_IndexInHeritedWidgetState createState() => _IndexInHeritedWidgetState();}class _IndexInHeritedWidgetState extends State<IndexInHeritedWidget> {UserBean userBean = UserBean(name: "default", address: "default");num i = 1;@overrideWidget build(BuildContext context) {print("page1");return Scaffold(appBar: AppBar(title: Text("Page_1"),),body: NotificationListener<CustomNotification>(///CustomNotification(UserBean(/// name : "Flutter custom Notification",/// address: "China")///).dispatch(context);onNotification: (data){ // data 是监听 CustomNotification 变化的数据,如下面改变了的userBean = data.userBean;setState(() {});return true;},child : Builder(builder: (context) => Container(width: double.infinity,height: double.infinity,color: Color(0x88f1f1f1),child: Column(children: [SizedBox(height: 10.0,),Text( userBean.name ),SizedBox(height: 10.0,),Text( userBean.address ),SizedBox(height: 200.0,),FloatingActionButton(onPressed: (){CustomNotification(UserBean(name : "Flutter custom Notification $i",address: "China $i")).dispatch(context);/// 当 dispatch 的时候我们就会往上找,直到找到NotificationListener的onNotification然后改变值。setState(() {i++;});},child: Icon(Icons.add_alert_outlined),),SizedBox(height: 200.0,),],),),)),);}}
CustomNotification
import 'package:YuWeiFlutterApp/src/userbean.dart';import 'package:flutter/material.dart';// Notificationclass CustomNotification extends Notification {UserBean userBean;CustomNotification(this.userBean);}
