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!');
},
),
);
}
āļø 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. -
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.