当前位置:网站首页>flutter EventBus
flutter EventBus
2022-07-15 23:42:00 【氤氲息】
定义好这个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();
}
边栏推荐
- Interface test - process test supports batch parameter import, and the test efficiency is directly full
- Course design of traffic light single chip microcomputer
- 深度学习模型参数量以及FLOPs计算工具
- Database Chapter III operation
- Digital display of potentiometer based on ADC0832
- What are MTTF, MTBF, mtrf?
- Add / remove MySQL index stored procedure
- OLED clock
- Burning firmware of Hongmeng openharmony system for rk3568 development board
- Envoy monitoring management
猜你喜欢

Flink(二)时间和窗口

Calculate the average wage excluding the maximum wage and the minimum wage of the Department (ByteDance interview)

Leetcode day 22

Flink (V) status programming

4 * 4 * 4 Light Cube production and sharing

Wireshark packet capturing debugging (three-way handshake, four-way disconnection)

Summary of process synchronization problems

低代码渲染那些事

Flink (III) processing function

Emqx server establishes ssl/tls secure connection, one-way and two-way
随机推荐
Design of DC motor control system
Addition, deletion and modification of elements [DOM (II)]
曲伟海:坚持选择不放弃 是实现初心的法宝
Pbft brief introduction
剑指 Offer 64. 求 1 + 2 + … + n
Autojs script notes
Redis crash debugging
Offre de doigts 64. Trouver 1 + 2 +... + n
Experiment 3 servlet related technology
元素的增删改查【DOM(二)】
JVM简述 GC垃圾回收机制
Breathing lamp circuit based on 555 timer
微信小程序在加载中时如何显示提示框?
Experiment 2 image enhancement
[unity3d] toggle of ugui
openpcdet之pointpillar代码阅读——第一篇:数据增强与数据处理
Envoy monitoring management
subset sum problem
LCD1602 display key position
Ultrasonic ranging OLED display