relay 0.3.7

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

Relay #

Library which uses modified BLoC pattern to listen to multiple updates in single builder widget. #

Import #

import 'package:relay/relay.dart';

Example #

Through simple implementation you can relay update from station and subscribe via RelayBuilder.

RelayBuilder Widget can listen to more than one updates you can provide observers parameter a list of updates.

Like in below example,

  • first relay builder widget observes only on counter.
  • second relay builder widget observes on both counter and name.
import 'dart:async';

import 'package:flutter/material.dart' hide Action;
import 'package:relay/relay.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Provider(
      manager: StoreManager(
        stores: {
          ExampleStore: () => ExampleStore(),
        },
      ),
      child: MaterialApp(
        title: 'Relay Example',
        home: Example(),
      ),
    );
  }
}

class IncrementAction extends Action {
  IncrementAction() : super(null);
}

class NameAction extends Action {
  NameAction(String name) : super(name);
}

class CounterUpdate extends Update {
  CounterUpdate(int counter) : super(counter);
}

class NameUpdate extends Update {
  NameUpdate(String name) : super(name);
}

class MessageUpdate extends Update {
  MessageUpdate(String message) : super(message);
}

class ExampleStore extends Store {
  int counter = 0;

  @override
  Stream<Update> onAction(Action action) async* {
    if (action is IncrementAction) {
      if (counter < 10) {
        counter++;
        yield CounterUpdate(counter);
      } else {
        final snackBarMessage = 'Maximum Limit Reached';
        yield MessageUpdate(snackBarMessage);
      }
    } else if (action is NameAction) {
      yield NameUpdate(action.params);
    }
  }
}

class Example extends StatefulWidget {
  @override
  ExampleState createState() => ExampleState();
}

class ExampleState extends State<Example> with ProviderMixin<ExampleStore> {
  void onUpdate(Update update) {
    if (update is MessageUpdate)
      Scaffold.of(context).showSnackBar(SnackBar(content: Text(update.data)));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: Column(
            children: <Widget>[
              RelayBuilder<ExampleStore>( // Only Listen To Single Update.
                observer: CounterUpdate,
                builder: (context, data) => Text(data),
              ),
              MultiRelayBuilder<ExampleStore>( // Listen To Multiple Updates, data will be Map<Type,dynamic>.
                observers: [NameUpdate, CounterUpdate],
                builder: (context, data) =>
                    Text('${data[NameUpdate]} : ${data[CounterUpdate]}'),
              ),
              Dispatcher<ExampleStore>( // Getting Store For Dispatching The Actions.
                builder: (context, store) => TextField(
                  onChanged: (name) => store.dispatchAction(NameAction(name)),
                  decoration: InputDecoration(
                    labelText: 'Name',
                  ),
                ),
              )
            ],
          ),
        ),
      ),
      floatingActionButton: Dispatcher<ExampleStore>(
        builder: (context, store) => FloatingActionButton(
          onPressed: () => store.dispatchAction(IncrementAction()),
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}
  • Widget

Extend ProviderWidget and ProviderState then you can access the store object in deep hierarchies also.

[0.3.7] - New Features:

  • Added Initial Updates on Widget Requests.

[0.3.6] - Major Api Breaking Changes :

  • Separated Two Classes : Single Update -> RelayBuilder, Multiple Update -> MultiRelayBuilder

[0.3.5] - Bug Fixes :

  • Removed Immutable Warning.

[0.3.2] - New Features :

  • Removed Parameter Store From Relay Builder( You can explicitly specify it also).
  • Dispatcher Widget can be used to get store object for dispatching actions.

[0.3.1] - Bug Fixes :

  • Fixed Counter Bug in Example.

[0.3.0] - Major Api Breaking Changes :

  • Added New Class : Action and Update
  • Used Stream and yield to release update.
  • Changes are within updates. No need of store in build methods.

[0.2.1] - Major Api Breaking Changes :

  • Renamed Station to Store.
  • Provider Widgets are decoupled using ProviderMixin.
  • Store Initialization made global and uplifted to Provider Widget.

[0.1.4] - Major Bug Fixes:

Bug of re-initialization of station object fixed.

[0.1.2] - New Function :

Added Support for relaying more than one updates at a time via relayMultiple() function.

[0.1.0] - Stable Release :

Added Support for listening the updates via onUpdate method. It can be used for pushing routes or showing snackbar. See More in Example

[0.0.1] - Initial Release.

example/main.dart

import 'dart:async';

import 'package:flutter/material.dart' hide Action;
import 'package:relay/relay.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Provider(
      manager: StoreManager(
        stores: {
          ExampleStore: () => ExampleStore(),
        },
      ),
      child: MaterialApp(
        title: 'Relay Example',
        home: Example(),
      ),
    );
  }
}

class IncrementAction extends Action {
  IncrementAction() : super(null);
}

class NameAction extends Action {
  NameAction(String name) : super(name);
}

class CounterUpdate extends Update {
  CounterUpdate(int counter) : super(counter);
}

class NameUpdate extends Update {
  NameUpdate(String name) : super(name);
}

class MessageUpdate extends Update {
  MessageUpdate(String message) : super(message);
}

class ExampleStore extends Store {
  int counter = 0;

  @override
  Stream<Update> onAction(Action action) async* {
    if (action is IncrementAction) {
      if (counter < 10) {
        counter++;
        yield CounterUpdate(counter);
      } else {
        final snackBarMessage = 'Maximum Limit Reached';
        yield MessageUpdate(snackBarMessage);
      }
    } else if (action is NameAction) {
      yield NameUpdate(action.params);
    }
  }
}

class Example extends StatefulWidget {
  @override
  ExampleState createState() => ExampleState();
}

class ExampleState extends State<Example> with ProviderMixin<ExampleStore> {
  void onUpdate(Update update) {
    if (update is MessageUpdate)
      Scaffold.of(context).showSnackBar(SnackBar(content: Text(update.data)));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: Column(
            children: <Widget>[
              RelayBuilder<ExampleStore>(
                observer: CounterUpdate,
                builder: (context, data) => Text(data),
              ),
              MultiRelayBuilder<ExampleStore>(
                observers: [NameUpdate, CounterUpdate],
                builder: (context, data) =>
                    Text('${data[NameUpdate]} : ${data[CounterUpdate]}'),
              ),
              Dispatcher<ExampleStore>(
                builder: (context, store) => TextField(
                  onChanged: (name) => store.dispatchAction(NameAction(name)),
                  decoration: InputDecoration(
                    labelText: 'Name',
                  ),
                ),
              )
            ],
          ),
        ),
      ),
      floatingActionButton: Dispatcher<ExampleStore>(
        builder: (context, store) => FloatingActionButton(
          onPressed: () => store.dispatchAction(IncrementAction()),
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  relay: ^0.3.7

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:relay/relay.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Nov 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test