定义好这个eventbus工具
可以在里面定义事件
import 'dart:async';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:social_im/models/im_customInfo_model.dart';
import 'package:social_im/pages/IMCCalling/ui/base/CallTypes.dart';
import 'package:social_im/pages/IMCCalling/ui/base/CallingScenes.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_conversation.dart';
class EventBusUtil {
static EventBus? _eventBus;
//获取单例
static EventBus getInstance() {
if (_eventBus == null) {
_eventBus = EventBus();
}
return _eventBus!;
}
//返回某事件的订阅者
static StreamSubscription<T> listen<T extends Event>(
Function(T event) onData) {
if (_eventBus == null) {
_eventBus = EventBus();
}
//内部流属于广播模式,可以有多个订阅者
return _eventBus!.on<T>().listen(onData);
}
//发送事件
static void fire<T extends Event>(T e) {
if (_eventBus == null) {
_eventBus = EventBus();
}
_eventBus!.fire(e);
}
}
abstract class Event {}
class LoginEvent extends Event {
static int eventIn = 1;
static int eventOut = 0;
static int onKickedOffline = 2;
static int onUserSigExpired = 3;
final int status;
LoginEvent(this.status);
}
class LoginSuccessdEvent extends Event {}
class MomentListEvent extends Event {}
class ObtainingLocationPermissionEvent extends Event {}
class PhotoListEvent extends Event {
int photoType = 0;
///0上传 1删除
PhotoListEvent(this.photoType);
}
class PhotoPayEvent extends Event {
int photoId = 0;
PhotoPayEvent(this.photoId);
}
class VideoListEvent extends Event {
int videoType = 0;
///0上传 1删除
VideoListEvent(this.videoType);
}
class VideoPayEvent extends Event {
int videoId = 0;
VideoPayEvent(this.videoId);
}
class StowKeyboardEvent extends Event {}
class MomentEvent extends Event {
final bool? isLike;
final int? commentCount;
final int? likeCount;
MomentEvent({this.isLike, this.commentCount, this.likeCount});
}
class MessageNoticeEvent extends Event {
final int? messageType;
final int? messageNum;
final int? activityType;
MessageNoticeEvent({this.messageType, this.messageNum,this.activityType});
}
class MessageEvent extends Event {
final V2TimConversation v2timConversation;
MessageEvent({required this.v2timConversation});
}
class CallingVideoEvent extends Event {
final CallingScenes callingVideoType;
CallingVideoEvent({required this.callingVideoType});
}
class ScreeningNearbyEvent extends Event {
RangeValues? rangeValues;
int? checkType;
ScreeningNearbyEvent({this.rangeValues, this.checkType});
}
class ImCustomInfoEvent extends Event {
ImCustomInfoModel? imCustomInfoModel;
ImCustomInfoEvent(this.imCustomInfoModel);
}
class MatchDialogEvent extends Event {
int matchType;
int matchUserId;
int playType;
MatchDialogEvent(this.matchType, this.matchUserId, this.playType);
}
class MatchPayNumDialogEvent extends Event {
Map<String, dynamic> payData;
MatchPayNumDialogEvent(this.payData);
}
class ShakeCloseDialogEvent extends Event{
bool isClose;
ShakeCloseDialogEvent(this.isClose);
}
class IsCanShakeEvent extends Event{
bool isCanShake;
IsCanShakeEvent(this.isCanShake);
}
使用:
发布者
//如果达到阈值,EventBus发布消息,让结果弹窗返回该界面,重新摇一摇
EventBusUtil.fire(ShakeCloseDialogEvent(true));
订阅者
late StreamSubscription<ShakeCloseDialogEvent> _shakeCloseDialogEvent;
@override
void initState() {
// TODO: implement initState
_shakeCloseDialogEvent = EventBusUtil.listen((event) {//不断监听,如果收到的是true就返回界面
if (event.isClose) {
Navigator.pop(context, true);
}
});
super.initState();
}
@override
void dispose() {
// TODO: implement dispose
_shakeCloseDialogEvent.cancel();//取消
super.dispose();
}