logo头像
Snippet 博客主题

event_bus 事件广播事件监听

1. 新建一文件 EventBus.dart

1
2
3
4
5
6
7
import 'package:event_bus/event_bus.dart'; 
//Bus 初始化
EventBus eventBus = EventBus();
class ProductContentEvent {
String text;
ProductContentEvent(String text){ this.text = text; }
}

2.在需要广播事件的页面引入上面的 EventBus.dart 类

1
eventBus.fire(new ProductContentEvent('购物车'));

3. 在需要监听广播的地方引入上面的 EventBus.dart

1
2
3
//监听广播
eventBus.on<ProductContentEvent>().listen((event){ print(event);
this._attrBottomSheet(); });

4.event_bus 取消事件监听

1
2
3
4
var actionSubscription =eventBus.on<ProductContentEvent>().listen((event){ print(event); 
this._attrBottomSheet();
});
actionSubscription.cancel();
微信打赏

赞赏是不耍流氓的鼓励