All Projects → dabit3 → flutter-redux-example

dabit3 / flutter-redux-example

Licence: other
Example app using Flutter with Redux including multiple reducers & initial app state

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 flutter-redux-example

LearningPoint
A repository for learning different technologies, frameworks, features......
Stars: ✭ 66 (+127.59%)
Mutual labels:  flutter-dart

Flutter Redux Example

Flutter example project using redux with multiple reducers & combineReducers.

Getting Started

To get started, clone the project:

git clone https://github.com/dabit3/flutter-redux-example.git

Redux files

model.dart

import 'package:flutter/foundation.dart';

class Item {
  final int id;
  final String body;

  Item({
    @required this.id,
    @required this.body
  });
}

class AppState {
  final List<Item> items;
  final int count;

  AppState({
    @required this.items,
    @required this.count,
  });

  AppState.initialState()
  : items = List.unmodifiable(<Item>[]),
    count = 0;
}

reducers.dart

import 'package:reduxexample/model.dart';
import 'package:reduxexample/redux/actions.dart';
import 'package:redux/redux.dart';

enum Actions { Increment }

List <Item> addItemReducer(List<Item> state, action) {
  return []
      ..addAll(state)
      ..add(Item(id: action.id, body: action.item));
}

List <Item> removeItemReducer(List<Item> state, action) {
  return List.unmodifiable(List.from(state)..remove(action.item));
}

List <Item> removeItemsReducer(List<Item> state, action) {
  return List.unmodifiable([]);
}

final Reducer <List<Item>> itemsReducer = combineReducers <List<Item>>([
  new TypedReducer<List<Item>, AddItemAction>(addItemReducer),
  new TypedReducer<List<Item>, RemoveItemAction>(removeItemReducer),
  new TypedReducer<List<Item>, RemoveItemsAction>(removeItemsReducer),
]);

int incrementReducer(int state, action) {
  if (action == Actions.Increment) {
    return state + 1;
  }
  return state;
}

AppState appStateReducer(AppState state, action) {
  return AppState(
    items: itemsReducer(state.items, action),
    count: incrementReducer(state.count, action),
  );
}

Creating the store

final Store store = Store<AppState>(
  appStateReducer,
  initialState: AppState.initialState(),
);

return StoreProvider<AppState>(
  store: store,
  child: new MaterialApp(
    title: 'Flutter App',
    theme: new ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: MyHomePage()
  ),
);
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].