zam_event_bus_provider 0.5.1 zam_event_bus_provider: ^0.5.1 copied to clipboard
A provider for flutter that passes EventBus down to all the widgets.
Event Bus Provider #
A flutter provider that passes EventBus
down to all the widgets.
NOTE: This package is an extension to zam_event_bus
.
What's inside the package #
Includes the following core components.
Check out all the components in detail here
How to use #
Step 1: Create an EventBus #
final bus = EventBus(transformers);
EventBus
is from zam_event_bus
package.
Step 2: Provide the EventBus #
final app = EventBusProvider(
bus: bus,
child: MaterialApp(
home: MyHomePage(title: 'Event Bus Demo'),
),
);
runApp(app);
Add EventBusProvider
before MaterialApp
so that it is made available to all the routes.
Step 3: Use context to dispatch events #
FloatingActionButton(
onPressed: () => context.dispatch(IncrementEvent()),
tooltip: 'Increment',
child: const Icon(Icons.add),
),
Step 4: Wrap your widget with View widget to listen to data #
View<Counter>(
builder: (data) => Text(
data.value.toString(),
style: Theme.of(context).textTheme.headline4,
),
)
You can also use StreamBuilder
to listen to data.
StreamBuilder<Counter>(
initialData: context.fetch<Counter>(),
stream: context.select<Counter>(),
builder: (context, snapshot) {
final counterText = snapshot.data!.value.toString();
return Text(
counterText,
style: Theme.of(context).textTheme.headline4,
);
},
)
Or you can create a widget extending DataWidget
.
class CounterText extends DataWidget<Counter> {
@override
Widget buildUsingData(BuildContext context, Counter data) {
return Text(
'Inheritance: ${data.value.toString()}',
style: Theme.of(context).textTheme.headline4,
);
}
}
To learn more, move on to the example section or check out these dedicated examples in github.