Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/limboy/bloc_redux

bloc meets redux in flutter world
https://github.com/limboy/bloc_redux

bloc dart flutter redux

Last synced: about 13 hours ago
JSON representation

bloc meets redux in flutter world

Awesome Lists containing this project

README

        

# Bloc_Redux

![](https://img.shields.io/pub/v/bloc_redux.svg)

Bloc_Redux is a combination of Bloc and Redux. OK, get it, but why?

Redux is an elegant pattern, which simplify the state management, and makes an unidirectional data flow. but it needs reducers return another state. by default it will make any widget using this state rebuild no matter if it has been modified. optimization can be made like diff to see the real changed properties, but not easy to do in flutter without reflection.

BLoC is a more general idea, UI trigger events using bloc's sink method, bloc handle it then change stream, UI receive latest stream value to reflect the change. it's vague on how to combine multi blocs or should there be only one per page? so it's more an idea than a spec.

so to get the most of both, here comes Bloc_Redux.

# Workflow

![](https://raw.githubusercontent.com/lzyy/bloc_redux/master/lib/bloc_redux/bloc_redux.png)

- actions are the only way to change state.
- blocs handle actions just like reducers but don't produce new states.
- widgets' data comes from state's stream, binded.

User trigger a button, it produces an action send to blocs, blocs which are interested in this action do some bussiness logic then change state by adding something new to stream, since streams are bind to widgets, UI are automatically updated.

# Detail

## bloc_redux.dart

```dart
/// Used by StateInput and Store.
/// When `StoreProvider` is disposed, it will call `BRStore`.dispose
/// which will call StateInput.dispose to close stream.
abstract class Disposable {
void dispose();
}

/// Useful when combined with StreamBuilder
class StreamWithInitialData {
final Stream stream;
final T initialData;

StreamWithInitialData(this.stream, this.initialData);
}

/// Action
///
/// every action should extends this class
abstract class BRAction {
T playload;
}

/// State
///
/// Input are used to change state.
/// usually filled with StreamController / BehaviorSubject.
/// handled by blocs.
///
/// implements disposable because stream controllers needs to be disposed.
/// they will be called within store's dispose method.
abstract class BRStateInput implements Disposable {}

/// Output are streams.
/// followed by input. like someController.stream
/// UI will use it as data source.
abstract class BRStateOutput {}

/// State
///
/// Combine these two into one.
abstract class BRState {
T input;
U output;
}

/// Bloc
///
/// like reducers in redux, but don't return a new state.
/// when they found something needs to change, just update state's input
/// then state's output will change accordingly.
typedef Bloc = void Function(BRAction action, T input);

/// Store
abstract class BRStore
implements Disposable {
List> blocs;
U state;

void dispatch(BRAction action) {
blocs.forEach((f) => f(action, state.input));
}

// store will be disposed when provider disposed.
dispose() {
state.input.dispose();
}
}
```

`StreamWithInitialData` is useful when consumed by `StreamBuilder` which needs `initialData`.

## store_provider.dart

borrowed from [here](https://www.didierboelens.com/2018/12/reactive-programming---streams---bloc---practical-use-cases/)

```dart
Type _typeOf() => T;

class StoreProvider extends StatefulWidget {
StoreProvider({
Key key,
@required this.child,
@required this.store,
}) : super(key: key);

final Widget child;
final T store;

@override
_StoreProviderState createState() => _StoreProviderState();

static T of(BuildContext context) {
final type = _typeOf<_StoreProviderInherited>();
_StoreProviderInherited provider =
context.ancestorInheritedElementForWidgetOfExactType(type)?.widget;
return provider?.store;
}
}

class _StoreProviderState extends State> {
@override
void dispose() {
widget.store?.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return new _StoreProviderInherited(
store: widget.store,
child: widget.child,
);
}
}

class _StoreProviderInherited extends InheritedWidget {
_StoreProviderInherited({
Key key,
@required Widget child,
@required this.store,
}) : super(key: key, child: child);

final T store;

@override
bool updateShouldNotify(_StoreProviderInherited oldWidget) => false;
}
```

# Demo

![](https://raw.githubusercontent.com/lzyy/bloc_redux/master/lib/bloc_redux/demo.gif)

## Color Demo

```dart
/// Actions
class ColorActionSelect extends BRAction {}

/// State
class ColorStateInput extends BRStateInput {
final BehaviorSubject selectedColor = BehaviorSubject();
final BehaviorSubject> colors = BehaviorSubject();

dispose() {
selectedColor.close();
colors.close();
}
}

class ColorStateOutput extends BRStateOutput {
StreamWithInitialData selectedColor;
StreamWithInitialData> colors;

ColorStateOutput(ColorStateInput input) {
selectedColor = StreamWithInitialData(
input.selectedColor.stream, input.selectedColor.value);
colors = StreamWithInitialData(input.colors.stream, input.colors.value);
}
}

class ColorState extends BRState {
ColorState() {
input = ColorStateInput();

var _colors = List.generate(
30, (int index) => ColorModel(RandomColor(index).randomColor()));
_colors[0].isSelected = true;
input.colors.add(_colors);

input.selectedColor.add(_colors[0].color);
output = ColorStateOutput(input);
}
}

/// Blocs
Bloc colorSelectHandler = (action, input) {
if (action is ColorActionSelect) {
input.selectedColor.add(action.playload);
var colors = input.colors.value
.map((colorModel) => colorModel
..isSelected = colorModel.color.value == action.playload.value)
.toList();
input.colors.add(colors);
}
};

/// Store
class ColorStore extends BRStore {
ColorStore() {
state = ColorState();
blocs = [colorSelectHandler];
}
}
```

State is separated into `input` and `output`, `input` is used by `blocs`, if `bloc` find it's necessary to change state, it can add something new to stream. widgets will receive this change immediately by listening `output`.