signal 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new52

signal #

Stream-based multiple state management. Multiple state management in one channel. Similar to the bloc structure, but current states are always accessible.

Usage #

Create channel and its signal.
abstract class MyChannelSignal extends ChannelSignal{}

class MyChannel extends StateChannel<MyChannelSignal>{
 
}

Create states and its signals

class CounterStateSignal extends MyChannelSignal{}

class CounterState extends BaseState{
  CounterState(void Function() onStateChanged) : super(onStateChanged);
 
 int _count =0;
 int get count => _count;

  @override
  initState() {
    _count=0;
    doneSucces(signal: false);
  }

 @override
  dispose() {
  }

  void increment() {
    _count = _count + 1;
    doneSucces();
  }

  void decrement() {
    _count = _count - 1;
     doneSucces();
  }

   incrementFuture() async {
    try {
       wait();

      await Future<void>.delayed(Duration(milliseconds: 400));
      _count = _count + 1;

     doneSucces();
    } catch (e) {
      doneError(e.toString());
    }
  }

    decrementFuture() async {
    try {
     wait();

      await Future<void>.delayed(Duration(milliseconds: 400));
      _count = _count - 1;

      doneSucces();
    } catch (e) {
      doneError(e.toString());
    }
  }
}
 



class NotificationStateSignal extends MyChannelSignal{}

class NotificationState extends BaseState{
  NotificationState(void Function() onStateChanged) : super(onStateChanged);

....

}



class ColorStateSignal extends MyChannelSignal{}

class ColorState extends BaseState{
  ColorState(void Function() onStateChanged) : super(onStateChanged);
 
....

}

Add states to the channel.

abstract class MyChannelSignal extends ChannelSignal{}

class MyChannel extends StateChannel<MyChannelSignal>{

  MyChannel() {
    _counterState = CounterState(() => add(CounterStateSignal()));
    _notificationState = NotificationState(() => add(NotificationStateSignal()));
    _colorState = ColorState(() => add(ColorStateSignal()));
  }

//signal: CounterStateSignal
  CounterState _counterState;
  CounterState get counterState => _counterState;

//signal: NotificationStateSignal
  NotificationState _notificationState;
  NotificationState get notificationState => _notificationState;

//signal: ColorStateSignal
  ColorState _colorState;
  ColorState get colorState => _colorState;


  @override
  initState() {
     super.initState();
     _counterState.initState();
  }
 
}

AncestorChannelProvider

Creates a channel, store it, and expose it to its descendants. A AncestorChannelProvider manages the lifecycle of the channel.


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

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    return AncestorChannelProvider<MyChannel>(
      channel: MyChannel(),
      child: MaterialApp(
        title: 'Signal State Management',
        home: MyHomePage(),
      ),

    );
  }
}

AncestorChannelBuilder

AncestorChannelBuilder handles building a widget in response to new ChannelSignal broadcasting from AncestorChannelProvider on an ancestor.


AncestorChannelBuilder<MyChannel, MyChannelSignal>(
    condition: (channel, signal) => signal is CounterStateSignal,
    builder: (context, channel) => 
      channel.counterState.busy ? CircularProgressIndicator():
      !channel.counterState.success ? Text(channel.notificationState.error) :
      Text(channel.counterState.count.toString()),
  ),

OwnChannelBuilder

OwnChannelBuilder creates a new Channel objec and handles building a widget in response to new StateSignal. OwnChannelBuilder does not expose it to its descendants.


class OtherWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
   final mychannel = AncestorChannelProvider.of<MyChannel>(context);
 ...

   OwnChannelBuilder<MyChannel,MyChannelSignal>(
      channel: MyChannel(),
      condition: (channel,signal) =>signal is CounterStateSignal,
      builder: (context, channel) =>
       channel.counterState.busy ? CircularProgressIndicator():
      !channel.counterState.success ? Text(channel.counterStateSignal.error) :
      Text(  channel.counterState.count.toString(),),
  ),
  
 ...
  }
}

AvailableChannelBuilder

AvailableChannelBuilder handles building a widget in response to new ChannelSignal broadcasting from existing in an Widget scope.


class _MyHomePageState extends State<MyHomePage> {

  MyChannel availableMychannel;

@override
void initState() {
  super.initState();
  availableMychannel =MyChannel()..initState();
}
@override
void dispose() {
 availableMychannel.dispose();
  super.dispose();
}
 
  @override
  Widget build(BuildContext context) {

 ...

     AvailableChannelBuilder<MyChannel,MyChannelSignal>(
        channel: availableMychannel,
        condition: (channel,signal) =>signal is CounterStateSignal,
        builder: (context, channel) =>
          channel.counterState.busy ? CircularProgressIndicator():
          !channel.counterState.success ? Text(channel.counterStateSignal.error) :
          Text(  channel.counterState.count.toString(),),
    ),
  
 ...
  }
}

that's all.

[0.1.0] - 2019.07.12 #

  • The initState method has been added to the StateChannel class.
  • SetState, initState, dispose methods have been added to the BaseState class.
  • 'signal' option has been added to wait, doneSucces, doneError methods of BaseState class.

[0.0.3] - 2019.06.30 #

  • New description added.

[0.0.2] - 2019.06.30 #

  • New description added.

[0.0.1] - 2019.06.30 #

  • TODO: Describe initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:signal/signal.dart';
import 'MyChannel.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AncestorChannelProvider<MyChannel>(
      channel: MyChannel(),
      child: MaterialApp(
        title: 'Signal State Management',
        home: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  MyChannel availableMychannel;

  @override
  void initState() {
    super.initState();
    availableMychannel = MyChannel()..initState();
  }

  @override
  void dispose() {
    availableMychannel.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final mychannel = AncestorChannelProvider.of<MyChannel>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text("Signal State Management"),
      ),
      body: ListView(shrinkWrap: true, children: <Widget>[
        AncestorChannelBuilder<MyChannel, MyChannelSignal>(
          condition: (channel, signal) =>
              signal is NotificationStateSignal || signal is ColorStateSignal,
          builder: (context, channel) => SwitchListTile(
            subtitle: Text('Subtitle Text color from ColorState',
                style:
                    TextStyle(color: channel.colorState.color.withAlpha(255))),
            secondary: Icon(channel.notificationState.isOpen
                ? Icons.notifications_active
                : Icons.notifications_off),
            title: channel.notificationState.busy
                ? LinearProgressIndicator()
                : !channel.notificationState.success
                    ? Text(channel.notificationState.error)
                    : Text(channel.notificationState.isOpen
                        ? 'Notification Open'
                        : 'Notification Closed'),
            value: channel.notificationState.isOpen,
            onChanged: (bool value) => channel.notificationState.change(),
          ),
        ),
        Container(
          alignment: Alignment.center,
          height: 50,
          child: AncestorChannelBuilder<MyChannel, MyChannelSignal>(
            condition: (channel, signal) => signal is CounterStateSignal,
            builder: (context, channel) => channel.counterState.busy
                ? Container(
                    width: 50,
                    height: 50,
                    child: CircularProgressIndicator(
                      strokeWidth: 2,
                    ))
                : !channel.counterState.success
                    ? Text(channel.notificationState.error)
                    : Text(channel.counterState.count.toString(),
                        style: TextStyle(fontSize: 25)),
          ),
        ),
        AncestorChannelBuilder<MyChannel, MyChannelSignal>(
          condition: (channel, signal) => signal is ColorStateSignal,
          builder: (context, channel) => Container(
            height: 20,
            alignment: Alignment.center,
            child: channel.colorState.busy
                ? LinearProgressIndicator()
                : !channel.colorState.success
                    ? Text(channel.notificationState.error)
                    : Text(channel.colorState.color.toString(),
                        style: TextStyle(fontSize: 20)),
          ),
        ),
        AvailableChannelBuilder<MyChannel, MyChannelSignal>(
          channel: availableMychannel,
          condition: (channel, signal) => signal is CounterStateSignal,
          builder: (context, channel) => Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Container(
                width: 50,
                height: 50,
                alignment: Alignment.center,
                child: channel.counterState.busy
                    ? Container(
                        width: 20,
                        height: 20,
                        child: CircularProgressIndicator(
                          strokeWidth: 2,
                        ))
                    : !channel.counterState.success
                        ? Text(channel.notificationState.error)
                        : Text(channel.counterState.count.toString(),
                            style: TextStyle(fontSize: 25)),
              ),
              RaisedButton(
                  child: Text(
                      'AvailableChannelBuilder : CounterState : increment'),
                  onPressed: () => channel.counterState.increment()),
              RaisedButton(
                  child: Text(
                      'AvailableChannelBuilder : CounterState : decrementFuture'),
                  onPressed: () => channel.counterState.decrementFuture()),
            ],
          ),
        ),
        SizedBox(height: 10),
        OwnChannelBuilder<MyChannel, MyChannelSignal>(
          channel: MyChannel(),
          condition: (channel, signal) => signal is CounterStateSignal,
          builder: (context, channel) => Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Container(
                width: 50,
                height: 50,
                alignment: Alignment.center,
                child: channel.counterState.busy
                    ? Container(
                        width: 20,
                        height: 20,
                        child: CircularProgressIndicator(
                          strokeWidth: 2,
                        ))
                    : !channel.counterState.success
                        ? Text(channel.counterState.error)
                        : Text(channel.counterState.count.toString(),
                            style: TextStyle(fontSize: 25)),
              ),
              RaisedButton(
                  child: Text('OwnChannelBuilder : CounterState : increment'),
                  onPressed: () => channel.counterState.increment()),
              RaisedButton(
                  child: Text(
                      'OwnChannelBuilder : CounterState : decrementFuture'),
                  onPressed: () => channel.counterState.decrementFuture()),
            ],
          ),
        ),
        RaisedButton(
            child: Text('NotificationState : change '),
            onPressed: () => mychannel.notificationState.change()),
        RaisedButton(
            child: Text('NotificationState : changeFuture '),
            onPressed: () => mychannel.notificationState.changeFuture()),
        RaisedButton(
            child: Text('CounterState : increment'),
            onPressed: () => mychannel.counterState.increment()),
        RaisedButton(
            child: Text('CounterState : decrementFuture'),
            onPressed: () => mychannel.counterState.decrementFuture()),
        RaisedButton(
            child: Text('ColorState : changeColor'),
            onPressed: () => mychannel.colorState.changeColor()),
        RaisedButton(
            child: Text('ColorState : changeColorFuture'),
            onPressed: () => mychannel.colorState.changeColorFuture()),
      ]),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  signal: ^0.1.0

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:signal/signal.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
5
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]
52
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • signal that is a package requiring null.

Health suggestions

Format lib/src/BaseState.dart.

Run flutter format to format lib/src/BaseState.dart.

Format lib/src/StateChannel.dart.

Run flutter format to format lib/src/StateChannel.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test