relay 0.2.1

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.
enum ExampleUpdate { counter, name, message }

class ExampleStore extends Store<ExampleUpdate> {
  int counter = 0;
  String name = '';
  String snackBarMessage;

  void increment() {
    if (counter < 10) {
      counter++;
      relay(ExampleUpdate.counter);
    } else {
      snackBarMessage = 'Maximum Limit Reached';
      relay(ExampleUpdate.message);
    }
  }

  void updateName(String text) {
    name = text;
    relay(ExampleUpdate.name);
  }
}

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

class ExampleState extends State<Example>
    with ProviderMixin<ExampleStore, ExampleUpdate> {
  void onUpdate(ExampleUpdate update) {
    if (update == ExampleUpdate.message)
      Scaffold.of(context).showSnackBar(
          SnackBar(content: Text(getStore(context).snackBarMessage)));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: Column(
            children: <Widget>[
              RelayBuilder<ExampleStore, ExampleUpdate>(
                store: getStore(context),
                observers: [ExampleUpdate.counter],
                builder: (context, store) => Text('${store.counter}'),
              ),
              RelayBuilder<ExampleStore, ExampleUpdate>(
                store: getStore(context),
                observers: [ExampleUpdate.name, ExampleUpdate.counter],
                builder: (context, store) =>
                    Text('${store.name} : ${store.counter}'),
              ),
              TextField(
                onChanged: getStore(context).updateName,
                decoration: InputDecoration(
                  labelText: 'Name',
                ),
              )
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: getStore(context).increment,
        child: Icon(Icons.add),
      ),
    );
  }
}

Example With Use of Provider. #

  • main.dart

    Wrap Material App with Provider

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

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

[0.0.1] - Initial Release.

[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.1.2] - New Function :

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

[0.1.4] - Major Bug Fixes:

Bug of re-initialization of station object fixed.

[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.

example/main.dart

import 'package:flutter/material.dart';
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(),
      ),
    );
  }
}

enum ExampleUpdate { counter, name, message }

class ExampleStore extends Store<ExampleUpdate> {
  int counter = 0;
  String name = '';
  String snackBarMessage;

  void increment() {
    if (counter < 10) {
      counter++;
      relay(ExampleUpdate.counter);
    } else {
      snackBarMessage = 'Maximum Limit Reached';
      relay(ExampleUpdate.message);
    }
  }

  void updateName(String text) {
    name = text;
    relay(ExampleUpdate.name);
  }
}

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

class ExampleState extends State<Example>
    with ProviderMixin<ExampleStore, ExampleUpdate> {
  void onUpdate(ExampleUpdate update) {
    if (update == ExampleUpdate.message)
      Scaffold.of(context).showSnackBar(
          SnackBar(content: Text(getStore(context).snackBarMessage)));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: Column(
            children: <Widget>[
              RelayBuilder<ExampleStore, ExampleUpdate>(
                store: getStore(context),
                observers: [ExampleUpdate.counter],
                builder: (context, store) => Text('${store.counter}'),
              ),
              RelayBuilder<ExampleStore, ExampleUpdate>(
                store: getStore(context),
                observers: [ExampleUpdate.name, ExampleUpdate.counter],
                builder: (context, store) =>
                    Text('${store.name} : ${store.counter}'),
              ),
              TextField(
                onChanged: getStore(context).updateName,
                decoration: InputDecoration(
                  labelText: 'Name',
                ),
              )
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: getStore(context).increment,
        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.2.1

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]
37
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]
68
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

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
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test

Admin