event_hub 0.2.1 event_hub: ^0.2.1 copied to clipboard
Dispatch and listen events everywhere and whenever. Also know as `Event Bus`.
import 'package:event_hub/event_hub.dart';
import 'package:flutter/material.dart';
//
// 1. Create a global eventHub
//
EventHub eventHub = EventHub();
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Event Hub Example',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override
void initState() {
super.initState();
//
// 2. Register the event listener
//
eventHub.on('increase-counter', (_) {
setState(() {
_counter++;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Event Hub Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.displayLarge,
),
],
),
),
floatingActionButton: CustomizedFab(),
);
}
}
class CustomizedFab extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: () {
//
// 3. Fire the event
//
eventHub.fire('increase-counter');
},
tooltip: 'Increment',
child: Icon(Icons.add),
);
}
}