flutter EventBus使用

import 'dart:async';

import 'package:event_bus/event_bus.dart';
import 'package:flutter/widgets.dart';

/// EventBus的工具類
class EventBusUtils {
  // 單列模式
  static EventBus _eventBus;

  static EventBus shared() {
    if (_eventBus == null) {
      _eventBus = EventBus(); // 創建事件總線
    }
    return _eventBus;
  }

  /// 訂閱者
  static Map<Type, List<StreamSubscription>> subscriptions = {};

  /// 添加監聽事件
  /// [T] 事件泛型 必須要傳
  /// [onData] 接受到事件
  /// [autoManaged] 自動管理實例,off 取消
  static StreamSubscription on<T extends Object>(void onData(T event),
      {Function onError,
        void onDone(),
        bool cancelOnError,
        bool autoManaged = true}) {
    StreamSubscription subscription = shared()?.on<T>()?.listen(onData,
        onError: onError, onDone: onDone, cancelOnError: cancelOnError);
    if (autoManaged == true) {
      if (subscriptions == null) subscriptions = {};
      List<StreamSubscription> subs = subscriptions[T.runtimeType] ?? [];
      subs.add(subscription);
      subscriptions[T.runtimeType] = subs;
    }
    return subscription;
  }

  /// 移除監聽者
  /// [T] 事件泛型 必須要傳
  /// [subscription] 指定
  static void off<T extends Object>({StreamSubscription subscription}) {
    if (subscriptions == null) subscriptions = {};
    if (subscription != null) {
      // 移除傳入的
      List<StreamSubscription> subs = subscriptions[T.runtimeType] ?? [];
      subs.remove(subscription);
      subscriptions[T.runtimeType] = subs;
    } else {
      // 移除全部
      subscriptions[T.runtimeType] = null;
    }
  }

  /// 發送事件
  static void fire(event) {
    shared()?.fire(event);
  }
}

/// EventBus的工具類
/// 有狀態組件
mixin HosEventBusMixin<T extends StatefulWidget> on State<T> {
  /// 需要定義成全局的,共用一個是實例
  EventBus mEventBus = EventBusUtils.shared();

  /// 訂閱者
  List<StreamSubscription> mEventBusSubscriptions = [];

  /// 統一在這裏添加監聽者
  @protected
  void mAddEventBusListeners();

  /// 添加監聽事件
  void mAddEventBusListener<T>(void onData(T event),
      {Function onError, void onDone(), bool cancelOnError}) {
    mEventBusSubscriptions?.add(mEventBus?.on<T>()?.listen(onData,
        onError: onError, onDone: onDone, cancelOnError: cancelOnError));
  }

  /// 發送事件
  void mEventBusFire(event) {
    mEventBus?.fire(event);
  }

  @override
  @mustCallSuper
  void dispose() {
    super.dispose();
    debugPrint('dispose:HosEventBusMixin');
    if (mEventBusSubscriptions != null)
      for (StreamSubscription subscription in mEventBusSubscriptions) {
        subscription.cancel();
      }
  }

  @override
  @mustCallSuper
  void initState() {
    super.initState();
    debugPrint('initState:HosEventBusMixin');
    mAddEventBusListeners();
  }
}

//使用

//class ReFreshXXXEvent {
//  String text;
//
//  ReFreshFamilyMemberEvent(this.text);
//}

//發送事件
//EventBusUtils.fire(ReFreshXXXEvent("發送事件啦"));

//接收事件
///// 我一般把這個放到 initState() 方法裏
//HosEventBusUtils.on<RefreshXXXEvent>(
//(event) {
//if (event != null) {
//// 這裏需要好好理解 mounted
//if (mounted)
//setState(() {
//// 獲取傳過來的數據
//// print("eventbus傳過來的數據:" + event.text);
//// 在這裏執行相關的刷新操作,如刷新列表
//});
//}
//},
//);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章