Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

the typedeventnotifier library allows notifying listeners with an object.
listeners can be subscribed to only a special type or group of objects.

installation

add on pubspec.yml:

dependencies:
  typed_event_notifier: ... // latest package version

usage

see example in /example folder

import 'package:typed_event_notifier/typed_event_notifier.dart';


/// class [examplenotifier].
/// 
/// the example of notifier.
/// it can send notifications to listeners with an object
/// and notify listeners if they are registered for this object type
/// or extended objects.
class examplenotifier extends typedeventnotifier<event> {
  /// create [examplenotifier] instance.
  examplenotifier();
  
  /// will notify listeners with [currentpagechangedevent] event.
  void currentpage(int index) {
    _currentpage = index;
    notifylisteners(currentpagechangedevent(currentpage: currentpage));
  }

  /// will notify listeners with [pagesloadedevent] event.
  set loadedpages(set<int> set) {
    _loadedpages.addall(set);
    notifylisteners(pagesloadedevent(pages: set));
  }
}


//the part of example of listener on `current page changed` event only.
class _currentpageonlylistenerstate extends state<currentpageonlylistener> {
  string message = 'currentpageonly: empty';

  // will receive events only with currentpagechangedevent type.
  void currentpagechanged(currentpagechangedevent event) {
    setstate(() {
      message = 'currentpageonly: now current page is ${event.currentpage}';
    });
  }

  @override
  void initstate() {
    widget.notifier.addlistener(currentpagechanged);
    super.initstate();
  }

  @override
  void dispose() {
    widget.notifier.removelistener(currentpagechanged);
    super.dispose();
  }

  @override
  widget build(buildcontext context) {
    return text(message);
  }

}

// the part of example of listener on any event.
class _anylistenerstate extends state<anylistener> {
  string message = 'any: empty';

  // will receive events with currentpagechangedevent and pagesloadedevent type.
  void any(event event) {
    if (event is currentpagechangedevent) {
      setstate(() {
        message = 'any: now current page is ${event.currentpage}';
      });
    }
    if (event is pagesloadedevent) {
      setstate(() {
        message = 'any: new loaded pages is ${event.pages}';
      });
    }
  }

  @override
  void initstate() {
    widget.notifier.addlistener(any);
    super.initstate();
  }

  @override
  void dispose() {
    widget.notifier.removelistener(any);
    super.dispose();
  }

  @override
  widget build(buildcontext context) {
    return text(message);
  }
}

// the events for example, which will be sent through the notifier.
// they have abstract base class (used as parent type),
// and extends from it events.
// for example two types with different content.
/// class [event]. 
abstract class event {
  /// create [event] instance.
  event();
}

/// class [currentpagechangedevent].
class currentpagechangedevent extends event {
  /// index of current page.
  final int currentpage;

  /// create [currentpagechangedevent] instance.
  currentpagechangedevent({
    required this.currentpage,
  }) : super();
}

/// class [pagesloadedevent].
class pagesloadedevent extends event {
  /// indexes of loaded pages.
  final set<int> pages;

  /// create [pagesloadedevent] instance.
  pagesloadedevent({
    required this.pages,
  }) : super();
}

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.