数据传输一般处理方式:自上而下的一层一层传递数据。
场景: WidgetB数据改变后,WidgetA也随之作出响应。
解决方案1: 参考Flutter知识点: InheritedWidget
解决方案2:Notification,子节点状态变更,发送通知上报。
1.自定义notification
class TestNotification extends Notification {
TestNotification({ @required this.count,
});final int count;
}2.子节点发送通知
new RaisedButton(
textColor: Colors.black,
child: new Center(
child: new Text('点击传递随机数给上层Widget'),
),
onPressed: () { new TestNotification(count: new Random().nextInt(100)).dispatch(key.currentContext);
})3.父节点使用NotificationListener进行监听子节点发出的通知,并作出响应
new NotificationListener(
onNotification: (TestNotification n) {
scaffoldStateKey.currentState.showSnackBar(new SnackBar(content: new Text('随机数:${n.count}'))); return true;
},
child: new TestAPage(
key: key,
))效果图

11.jpeg
作者:老实巴交的读书人
链接:https://www.jianshu.com/p/d9e7e7ad4daa
随时随地看视频