项目作者: lzyy

项目描述 :
bloc meets redux in flutter world
高级语言: Dart
项目地址: git://github.com/lzyy/bloc_redux.git
创建时间: 2019-01-04T13:39:33Z
项目社区:https://github.com/lzyy/bloc_redux

开源协议:

下载


Bloc_Redux

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

  • 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

  1. /// Used by StateInput and Store.
  2. /// When `StoreProvider` is disposed, it will call `BRStore`.dispose
  3. /// which will call StateInput.dispose to close stream.
  4. abstract class Disposable {
  5. void dispose();
  6. }
  7. /// Useful when combined with StreamBuilder
  8. class StreamWithInitialData<T> {
  9. final Stream<T> stream;
  10. final T initialData;
  11. StreamWithInitialData(this.stream, this.initialData);
  12. }
  13. /// Action
  14. ///
  15. /// every action should extends this class
  16. abstract class BRAction<T> {
  17. T playload;
  18. }
  19. /// State
  20. ///
  21. /// Input are used to change state.
  22. /// usually filled with StreamController / BehaviorSubject.
  23. /// handled by blocs.
  24. ///
  25. /// implements disposable because stream controllers needs to be disposed.
  26. /// they will be called within store's dispose method.
  27. abstract class BRStateInput implements Disposable {}
  28. /// Output are streams.
  29. /// followed by input. like someController.stream
  30. /// UI will use it as data source.
  31. abstract class BRStateOutput {}
  32. /// State
  33. ///
  34. /// Combine these two into one.
  35. abstract class BRState<T extends BRStateInput, U extends BRStateOutput> {
  36. T input;
  37. U output;
  38. }
  39. /// Bloc
  40. ///
  41. /// like reducers in redux, but don't return a new state.
  42. /// when they found something needs to change, just update state's input
  43. /// then state's output will change accordingly.
  44. typedef Bloc<T extends BRStateInput> = void Function(BRAction action, T input);
  45. /// Store
  46. abstract class BRStore<T extends BRStateInput, U extends BRState>
  47. implements Disposable {
  48. List<Bloc<T>> blocs;
  49. U state;
  50. void dispatch(BRAction action) {
  51. blocs.forEach((f) => f(action, state.input));
  52. }
  53. // store will be disposed when provider disposed.
  54. dispose() {
  55. state.input.dispose();
  56. }
  57. }

StreamWithInitialData is useful when consumed by StreamBuilder which needs initialData.

store_provider.dart

borrowed from here

  1. Type _typeOf<T>() => T;
  2. class StoreProvider<T extends BRStore> extends StatefulWidget {
  3. StoreProvider({
  4. Key key,
  5. @required this.child,
  6. @required this.store,
  7. }) : super(key: key);
  8. final Widget child;
  9. final T store;
  10. @override
  11. _StoreProviderState<T> createState() => _StoreProviderState<T>();
  12. static T of<T extends BRStore>(BuildContext context) {
  13. final type = _typeOf<_StoreProviderInherited<T>>();
  14. _StoreProviderInherited<T> provider =
  15. context.ancestorInheritedElementForWidgetOfExactType(type)?.widget;
  16. return provider?.store;
  17. }
  18. }
  19. class _StoreProviderState<T extends BRStore> extends State<StoreProvider<T>> {
  20. @override
  21. void dispose() {
  22. widget.store?.dispose();
  23. super.dispose();
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. return new _StoreProviderInherited<T>(
  28. store: widget.store,
  29. child: widget.child,
  30. );
  31. }
  32. }
  33. class _StoreProviderInherited<T> extends InheritedWidget {
  34. _StoreProviderInherited({
  35. Key key,
  36. @required Widget child,
  37. @required this.store,
  38. }) : super(key: key, child: child);
  39. final T store;
  40. @override
  41. bool updateShouldNotify(_StoreProviderInherited oldWidget) => false;
  42. }

Demo

Color Demo

  1. /// Actions
  2. class ColorActionSelect extends BRAction<Color> {}
  3. /// State
  4. class ColorStateInput extends BRStateInput {
  5. final BehaviorSubject<Color> selectedColor = BehaviorSubject();
  6. final BehaviorSubject<List<ColorModel>> colors = BehaviorSubject();
  7. dispose() {
  8. selectedColor.close();
  9. colors.close();
  10. }
  11. }
  12. class ColorStateOutput extends BRStateOutput {
  13. StreamWithInitialData<Color> selectedColor;
  14. StreamWithInitialData<List<ColorModel>> colors;
  15. ColorStateOutput(ColorStateInput input) {
  16. selectedColor = StreamWithInitialData(
  17. input.selectedColor.stream, input.selectedColor.value);
  18. colors = StreamWithInitialData(input.colors.stream, input.colors.value);
  19. }
  20. }
  21. class ColorState extends BRState<ColorStateInput, ColorStateOutput> {
  22. ColorState() {
  23. input = ColorStateInput();
  24. var _colors = List<ColorModel>.generate(
  25. 30, (int index) => ColorModel(RandomColor(index).randomColor()));
  26. _colors[0].isSelected = true;
  27. input.colors.add(_colors);
  28. input.selectedColor.add(_colors[0].color);
  29. output = ColorStateOutput(input);
  30. }
  31. }
  32. /// Blocs
  33. Bloc<ColorStateInput> colorSelectHandler = (action, input) {
  34. if (action is ColorActionSelect) {
  35. input.selectedColor.add(action.playload);
  36. var colors = input.colors.value
  37. .map((colorModel) => colorModel
  38. ..isSelected = colorModel.color.value == action.playload.value)
  39. .toList();
  40. input.colors.add(colors);
  41. }
  42. };
  43. /// Store
  44. class ColorStore extends BRStore<ColorStateInput, ColorState> {
  45. ColorStore() {
  46. state = ColorState();
  47. blocs = [colorSelectHandler];
  48. }
  49. }

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.