ajwah_bloc 1.0.2 copy "ajwah_bloc: ^1.0.2" to clipboard
ajwah_bloc: ^1.0.2 copied to clipboard

outdated

Rx based state management library. Manage your application's states, effects, and actions easy way.

example/README.md

Please have a look at here for progressive examples

actionTypes.dart #

class ActionTypes {
  static const String Inc = 'inc';
  static const String Dec = 'dec';
  static const String AsyncInc = 'AsyncInc';
}

counterState.dart #

import 'package:ajwah_bloc/ajwah_bloc.dart';
import 'actionTypes.dart';

class CounterModel {
  final int count;
  final bool isLoading;
  CounterModel({this.count, this.isLoading});
  CounterModel.init() : this(count: 0, isLoading: false);
  CounterModel.countData(int count) : this(count: count, isLoading: false);
  CounterModel.loading(int count) : this(count: count, isLoading: true);
}

class CounterState extends BaseState<CounterModel> {
  CounterState() : super(name: 'counter', initialState: CounterModel.init());

  CounterModel reduce(CounterModel state, Action action) {
    switch (action.type) {
      case ActionTypes.Inc:
        return CounterModel.countData(state.count + 1);
      case ActionTypes.Dec:
        return CounterModel.countData(state.count - 1);
      case ActionTypes.AsyncInc:
        return CounterModel.loading(state.count);

      default:
        return state;
    }
  }
}

counterEffects.dart #

import 'package:ajwah_bloc/ajwah_bloc.dart';
import 'package:rxdart/rxdart.dart';

import 'actionTypes.dart';

class CounterEffects extends BaseEffect {
  Observable<Action> effectForAsyncInc(Actions action$, Store store$) {
    return action$
        .ofType(ActionTypes.AsyncInc)
        .debounceTime(Duration(milliseconds: 550))
        .mapTo(Action(type: ActionTypes.Inc));
  }

  List<Observable<Action>> registerEffects(Actions action$, Store store$) {
    return [effectForAsyncInc(action$, store$)];
  }
}

CounterComponent.dart #


import 'package:ajwah_bloc/ajwah_bloc.dart';
import 'actionTypes.dart';
import 'counterState.dart';
import 'package:flutter_web/material.dart';

class CounterComponent extends StatelessWidget {
  const CounterComponent({Key key}) : super(key: key);

  void increment() {
    dispatch(actionType: ActionTypes.Inc);
  }

  void decrement() {
    dispatch(actionType: ActionTypes.Dec);
  }

  void asyncIncrement() {
    dispatch(actionType: ActionTypes.AsyncInc);
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        ButtonBar(mainAxisSize: MainAxisSize.min, children: <Widget>[
          RaisedButton(
            textColor: Colors.white,
            color: Colors.blue,
            child: Icon(Icons.add),
            onPressed: increment,
          ),
          new RaisedButton(
            textColor: Colors.white,
            color: Colors.blue,
            child: Text('Async(+)'),
            onPressed: asyncIncrement,
          ),
          RaisedButton(
            textColor: Colors.white,
            color: Colors.blue,
            child: Icon(Icons.remove),
            onPressed: decrement,
          )
        ]),
        SizedBox(
          width: 10.0,
        ),
        StreamBuilder<CounterModel>(
          stream: store().select<CounterModel>(stateName: 'counter'),
          initialData: CounterModel.init(),
          builder:
              (BuildContext context, AsyncSnapshot<CounterModel> snapshot) {
                
            if (snapshot.data.isLoading) {
              return CircularProgressIndicator();
            }
            return Text(
              snapshot.data.count.toString(),
              style: Theme.of(context).textTheme.title,
            );
          },
        )
      ],
    );
  }
}

main.dart #

import 'package:ajwah_bloc/ajwah_bloc.dart';
import 'counterComponent.dart';
import 'counterState.dart';
import 'counterEffects.dart';
import 'package:flutter_web/material.dart';

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

class MyApp extends StatelessWidget {
  MyApp() {
    createStore(states: [CounterState()], effects: [CounterEffects()]);
    /*store().exportState().listen((arr) {
      print((arr[0] as Action).type);
      print(arr[1]);
    });
    store()
        .addState(SearchState())
        .removeStateByStateName('counter')
        .addState(CounterState());*/
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        debugShowCheckedModeBanner: false,
        home: CounterComponent());
  }
}

1
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Rx based state management library. Manage your application's states, effects, and actions easy way.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

async, meta, rxdart

More

Packages that depend on ajwah_bloc