event_bus_global 1.0.1
event_bus_global: ^1.0.1 copied to clipboard
Type-safe global event bus for Flutter.
import 'package:flutter/material.dart';
import 'package:event_bus_global/event_bus_global.dart';
final counterEvent = EventBusIdentifier<int>('counter');
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _total = 0;
ListenerDisposable? _subscription;
@override
void initState() {
super.initState();
_subscription = EventBusGlobal.event(counterEvent).listenManually((value) {
setState(() => _total += value);
}, sticky: true);
}
@override
void dispose() {
_subscription?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('event_bus_global')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
EventBusBuilder<int>(
event: counterEvent,
sticky: true,
builder: (context, value) => Text(
'${value ?? 0}',
style: const TextStyle(fontSize: 48),
),
),
const SizedBox(height: 24),
Text(
'Total (listener manual): $_total',
style: const TextStyle(fontSize: 16),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => EventBusGlobal.event(counterEvent).emit(1),
child: const Icon(Icons.add),
),
),
);
}
}