fix: 登录/退出登录页面刷新

This commit is contained in:
guozhigq
2023-08-19 15:25:00 +08:00
parent 7c2518bcd2
commit 2b0dc9d285
9 changed files with 206 additions and 81 deletions

53
lib/utils/event_bus.dart Normal file
View File

@ -0,0 +1,53 @@
// 订阅者回调签名
typedef void EventCallback(arg);
class EventBus {
// 私有构造函数
EventBus._internal();
// 保存单例
static final EventBus _singleton = EventBus._internal();
// 工厂构造函数
factory EventBus() => _singleton;
// 保存事件订阅者队列key:事件名(id)value: 对应事件的订阅者队列
final _emap = <dynamic, List<EventCallback>>{};
// 添加订阅者
void on(eventName, EventCallback f) {
_emap[eventName] ??= <EventCallback>[];
_emap[eventName]!.add(f);
}
// 移除订阅者
void off(eventName, [EventCallback? f]) {
var list = _emap[eventName];
if (eventName == null || list == null) return;
if (f == null) {
_emap[eventName] = [];
} else {
list.remove(f);
}
}
// 触发事件,事件触发后该事件所有订阅者会被调用
void emit(eventName, [arg]) {
var list = _emap[eventName];
if (list == null) return;
List<EventCallback> tempList = List.from(list);
for (var callback in tempList) {
callback(arg);
}
}
// 获取订阅者数量
int getSubscriberCount(eventName) {
var list = _emap[eventName];
return list?.length ?? 0;
}
}
class EventName {
static const String loginEvent = 'loginEvent';
}