All Projects → limboy → bloc_redux

limboy / bloc_redux

Licence: other
bloc meets redux in flutter world

Programming Languages

dart
5743 projects
objective c
16641 projects - #2 most used programming language
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to bloc redux

last fm
A simple app to demonstrate a testable, maintainable, and scalable architecture for flutter. flutter_bloc, get_it, hive, and REST API are some of the tech stacks used in this project.
Stars: ✭ 134 (+157.69%)
Mutual labels:  bloc
Blue-Diary
Lightweight & effective Todo app with Flutter and BLoC pattern 🙆🏻‍♂️
Stars: ✭ 146 (+180.77%)
Mutual labels:  bloc
smartrider
🧠🚕 The all-in-one RPI transportation app.
Stars: ✭ 42 (-19.23%)
Mutual labels:  bloc
flutter-checkio
How time flies.一款开源习惯打卡APP,流畅的动画体验,Bloc实现状态管理,主题(颜色)切换,字体切换,数据库管理等。
Stars: ✭ 507 (+875%)
Mutual labels:  bloc
bloc samples
A collection of apps built with the Bloc library.
Stars: ✭ 39 (-25%)
Mutual labels:  bloc
Chi Food
Food Delivery App made by Flutter and Bloc
Stars: ✭ 103 (+98.08%)
Mutual labels:  bloc
bloc
A predictable state management library that helps implement the BLoC design pattern
Stars: ✭ 12 (-76.92%)
Mutual labels:  bloc
flutter-bloc-patterns
A set of most common BLoC use cases built on top of flutter_bloc library
Stars: ✭ 58 (+11.54%)
Mutual labels:  bloc
travel app
Travel App using Flutter 💙
Stars: ✭ 74 (+42.31%)
Mutual labels:  bloc
movie-catalog
🎬 A movie catalog app for both Android & IOS ~ Flutter.io project in Dart | Dart, Bloc, Movies
Stars: ✭ 46 (-11.54%)
Mutual labels:  bloc
devon4flutter-non-bloc-arch
A guide aiming to bridge the gap between the absolute Flutter basics and clean, structured Flutter Development
Stars: ✭ 283 (+444.23%)
Mutual labels:  bloc
Flutter Roadmap
This is a flutter roadmap and documentation repository. If anyone is interested you can join the party to help the community and make flutter great again.
Stars: ✭ 47 (-9.62%)
Mutual labels:  bloc
flutter idiomatic
It is starter kit with idiomatic code structure :) Firebase Authentication & GraphQL CRUD via BLoC. With Unit tests, Widget tests and Integration tests as BDD.
Stars: ✭ 20 (-61.54%)
Mutual labels:  bloc
Wallbay
Wallpaper App developed in Flutter using Pexels API
Stars: ✭ 107 (+105.77%)
Mutual labels:  bloc
Movie-Flutter
Movie App Flutter using Flutter BLoC, DIO, Retrofit, Sqflite
Stars: ✭ 36 (-30.77%)
Mutual labels:  bloc
flutter-bloc
A compilation of flutter block state management tutorials
Stars: ✭ 55 (+5.77%)
Mutual labels:  bloc
whatsApp clone
Flutter WhatsClone (with Firebase + Clean Architecture) this app follow clean architecture proposed by our friendly Uncle Bob.
Stars: ✭ 181 (+248.08%)
Mutual labels:  bloc
daruma-frontend
🎎 Shared Expense Manager (Frontend) - Flutter app 🎎
Stars: ✭ 21 (-59.62%)
Mutual labels:  bloc
flutter preload videos
Preloading videos in Flutter 💙
Stars: ✭ 42 (-19.23%)
Mutual labels:  bloc
wholesome-cli
Command Line Tool for managing Flutter projects
Stars: ✭ 57 (+9.62%)
Mutual labels:  bloc

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

/// 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<T> {
  final Stream<T> stream;
  final T initialData;

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

/// Action
///
/// every action should extends this class
abstract class BRAction<T> {
  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 extends BRStateInput, U extends BRStateOutput> {
  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<T extends BRStateInput> = void Function(BRAction action, T input);

/// Store
abstract class BRStore<T extends BRStateInput, U extends BRState>
    implements Disposable {
  List<Bloc<T>> 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

Type _typeOf<T>() => T;

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

  final Widget child;
  final T store;

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

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

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

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

class _StoreProviderInherited<T> 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

Color Demo

/// Actions
class ColorActionSelect extends BRAction<Color> {}

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

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

class ColorStateOutput extends BRStateOutput {
  StreamWithInitialData<Color> selectedColor;
  StreamWithInitialData<List<ColorModel>> colors;

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

class ColorState extends BRState<ColorStateInput, ColorStateOutput> {
  ColorState() {
    input = ColorStateInput();

    var _colors = List<ColorModel>.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<ColorStateInput> 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<ColorStateInput, ColorState> {
  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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].