Created
July 29, 2024 15:17
-
-
Save lopo12123/46e2a8552af50a0bcab3911e991c2367 to your computer and use it in GitHub Desktop.
BadFL Example Code (impl/event_center)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
void handler1(dynamic eventData) { | |
print('in handler1: $eventData'); | |
} | |
void handler2(dynamic eventData) { | |
print('in handler2: $eventData'); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: ListView( | |
children: [ | |
ElevatedButton( | |
onPressed: () => BadEventCenter.listen('ev1', handler1), | |
child: const Text('add handler1'), | |
), | |
ElevatedButton( | |
onPressed: () => BadEventCenter.listen('ev1', handler2), | |
child: const Text('add handler2'), | |
), | |
ElevatedButton( | |
onPressed: () => BadEventCenter.unListen('ev1', handler2), | |
child: const Text('remove handler2'), | |
), | |
ElevatedButton( | |
onPressed: () => BadEventCenter.unListen('ev1', handler1), | |
child: const Text('remove handler1'), | |
), | |
ElevatedButton( | |
onPressed: () => BadEventCenter.unListen('ev1'), | |
child: const Text('remove all handler on ev1'), | |
), | |
ElevatedButton( | |
onPressed: () => BadEventCenter.unListenAll(), | |
child: const Text('remove all handler'), | |
), | |
ElevatedButton( | |
onPressed: () => BadEventCenter.trigger('ev1', 'data'), | |
child: const Text('trigger ev1'), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
typedef EventHandler = void Function(dynamic eventData); | |
typedef StopListen = void Function(); | |
/// `impl::event_center`: event center implementation | |
abstract class BadEventCenter { | |
static final Map<String, Set<EventHandler>> _handlers = {}; | |
/// add a [handler] on [eventName] | |
static StopListen listen(String eventName, EventHandler handler) { | |
var handlers = _handlers[eventName]; | |
if (handlers == null) { | |
_handlers[eventName] = {handler}; | |
} else { | |
handlers.add(handler); | |
} | |
return () { | |
handlers?.remove(handler); | |
}; | |
} | |
/// remove specific [handler] on [eventName], | |
/// if [handler] is null, remove all handlers on [eventName] | |
static void unListen(String eventName, [EventHandler? handler]) { | |
if (handler == null) { | |
_handlers.remove(eventName); | |
} else { | |
_handlers[eventName]?.remove(handler); | |
} | |
} | |
/// remove all handlers on all events | |
static void unListenAll() { | |
_handlers.clear(); | |
} | |
/// trigger all handlers on [eventName] with [eventData] | |
static void trigger(String evName, [dynamic eventData]) { | |
var handlers = _handlers[evName]; | |
if (handlers == null) { | |
return; | |
} | |
for (var handler in handlers) { | |
handler(eventData); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment