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 passBuildContextaround 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 ownnavigatorKeyandscaffoldMessengerKeyto control global navigation and SnackBar/Dialog handling. -
Zero Boilerplate Setup
Wrap yourMaterialAppinUIGlobalEventBus(or useEventBusMaterialApp), supply the two keys once, and you’re done—no more scatterednavigatorKeyorscaffoldMessengerKeyhacks. -
Built-In Pop Handling
Includes a dedicatedpop()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.
Navigate Parameter
-
page(Widget):
The destination widget to navigate to. -
navigateMethod(NavigationMethod):
The navigation action type:push– Push thepageonto the stackgo– Replace the current route with thepage- 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 totrue.
Usage
SnackBar
EventHelpers.showSnackBar('message');
Navigate
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.