UI Global Event Bus 🚌

A lightweight Flutter package that lets you dispatch and handle global UI events—SnackBars, navigation (Material Navigator), dialogs, and even arbitrary callbacks—from anywhere in your app with a single, centralized event bus.


📸 Preview



💻 Installation

Add the package to your pubspec.yaml:

dependencies:
  global_event_bus: ^1.0.0

or

flutter pub add global_event_bus

⚠️⚠️⚠️ Important: Required Setup ⚠️⚠️⚠️

Wrap your MaterialApp and set the keys

Example

void main() {
  runApp(
    UIGlobalEventBus(
      // ① wrap your MaterialApp
      MaterialApp(
        home: const EventEx1(),
        scaffoldMessengerKey: eventScaffoldKey,  // required
        navigatorKey: eventNavigateKey,          // required
      ),

      // ② optional custom callback
      customEvent: (BuildContext ctx) {
        print('Custom event triggered!');
      },

      // ③ If you have a custom key, set it up
      navigateKey: yourNavigateKey,
      scaffoldKey: yourScaffoldKey,
    ),
  );
}

⭐️ Features

  • One-Line UI Events
    Dispatch SnackBars, navigation (push/go), pop actions, dialogs, and custom callbacks with a single method call—no need to pass BuildContext around or wire up listeners everywhere.

  • Global Singleton Bus
    Behind the scenes it’s just a singleton broadcast stream, so you can fire events from anywhere and handle them in one central place.

  • Custom Key Configuration
    Configure your own navigatorKey and scaffoldMessengerKey to control global navigation and SnackBar/Dialog handling.

  • Zero Boilerplate Setup
    Wrap your MaterialApp in UIGlobalEventBus (or use EventBusMaterialApp), supply the two keys once, and you’re done—no more scattered navigatorKey or scaffoldMessengerKey hacks.

  • Built-In Pop Handling
    Includes a dedicated pop() event with optional result payload, so you don’t need separate code to pop the navigation stack.

  • Type-Safe & Extensible
    Clear method signatures and event classes keep your code compile-time checked, and you can add your own event types if needed.

⚙️ Details

Method Signature Description
showSnackBar(String message) showSnackBar(String message) Dispatches a SnackBar event
navigate({ required Widget page, required NavigationMethod navigateMethod = NavigationMethod.go }) navigate({ required Widget page, NavigationMethod navigateMethod = NavigationMethod.go}) Dispatches a navigation event (push/go) to the given page
pop({ dynamic extra }) pop({ dynamic extra }) Pops the current route; if extra is provided, it’s returned to the previous route
showDialog({ required Widget Function(BuildContext) builder, bool barrierDismissible = true }) showDialog({ required Widget Function(BuildContext) builder, bool barrierDismissible = true }) Dispatches a dialog event
customEvent() customEvent() Invokes the user-defined callback registered in UIGlobalEventBus

Parameters

SnackBar Parameter

  • message (String):
    The text to display in the SnackBar.
  • page (Widget):
    The destination widget to navigate to.

  • navigateMethod (NavigationMethod):
    The navigation action type:

    • push – Push the page onto the stack
    • go – Replace the current route with the page
    • Defaults to go.

Pop Parameter

  • extra (dynamic):
    Optional value returned when popping the current route.

Dialog Parameter

  • builder (Widget Function(BuildContext)):
    A function that returns the dialog widget.

  • barrierDismissible (bool):
    Whether tapping outside dismisses the dialog. Defaults to true.

Usage

SnackBar

EventHelpers.showSnackBar('message');
EventHelpers.navigate(
    page: const EventEx2(),
    navigateMethod: NavigationMethod.push,
);

Pop

EventHelpers.pop();

Dialog

EventHelpers.showDialog(
    builder: (ctx) => const AlertDialog(
        title: Text('title'),
        content: Text('context'),
    ),
);

Custom Event

EventHelpers.eventCustom();

📄 License

This project is licensed under the MIT License.
See the LICENSE file for details.