event_hub 0.1.1
event_hub: ^0.1.1 copied to clipboard

Dart native js
Flutter Android iOS web

Dispatch and listen events everywhere and whenever. Also know as `Event Bus`.

example/lib/main.dart

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>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      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),
    );
  }
}
6
likes
110
pub points
66%
popularity

Dispatch and listen events everywhere and whenever. Also know as `Event Bus`.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

devwingcom@gmail.com

License

MIT (LICENSE)

More

Packages that depend on event_hub