自习室优化ok
This commit is contained in:
33
lib/global/event_bus.dart
Normal file
33
lib/global/event_bus.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'dart:async';
|
||||
|
||||
/// 全局事件总线
|
||||
class EventBus {
|
||||
EventBus._();
|
||||
|
||||
static final _instance = EventBus._();
|
||||
|
||||
factory EventBus() => _instance;
|
||||
|
||||
|
||||
// StreamController,广播模式可以让多个地方监听
|
||||
final StreamController<GlobalEvent> _controller = StreamController.broadcast();
|
||||
|
||||
/// 发送事件
|
||||
void fire(GlobalEvent event) {
|
||||
_controller.add(event);
|
||||
}
|
||||
|
||||
/// 监听事件
|
||||
Stream<GlobalEvent> get stream => _controller.stream;
|
||||
|
||||
/// 关闭流(一般应用生命周期结束时调用)
|
||||
void dispose() {
|
||||
_controller.close();
|
||||
}
|
||||
}
|
||||
|
||||
/// 事件类型枚举
|
||||
enum GlobalEvent {
|
||||
unauthorized, // 401 需要重新登录
|
||||
// 以后可以扩展其他事件
|
||||
}
|
||||
Reference in New Issue
Block a user