diff --git a/example/lib/app.dart b/example/lib/app.dart index 26304c1..8b6421a 100644 --- a/example/lib/app.dart +++ b/example/lib/app.dart @@ -1,15 +1,26 @@ import 'package:flutter/material.dart'; import 'package:fish_redux/fish_redux.dart'; +import 'package:sample/global_store/state.dart'; +import 'global_store/global_store.dart'; import 'todo_edit_page/page.dart'; import 'todo_list_page/page.dart'; +//create global page helper +Page createGlobalPage>( + Page page) { + return page + ..connectExtraStore(GlobalStore.store, (T pagestate, GlobalState appState) { + return pagestate.clone(); + }); +} + Widget createApp() { final AbstractRoutes routes = HybridRoutes(routes: [ PageRoutes( pages: >{ - 'todo_list': ToDoListPage(), - 'todo_edit': TodoEditPage(), + 'todo_list': createGlobalPage(ToDoListPage()), + 'todo_edit': createGlobalPage(TodoEditPage()) }, ), ]); diff --git a/example/lib/global_store/action.dart b/example/lib/global_store/action.dart new file mode 100644 index 0000000..ea07ffe --- /dev/null +++ b/example/lib/global_store/action.dart @@ -0,0 +1,16 @@ +import 'dart:ui'; + +import 'package:fish_redux/fish_redux.dart'; + +//TODO replace with your own action +enum GlobalAction { action, changeThemeColor} + +class GlobalActionCreator { + static Action onAction() { + return const Action(GlobalAction.action); + } + + static Action onchangeThemeColor(Color color){ + return Action(GlobalAction.changeThemeColor, payload: color); + } +} diff --git a/example/lib/global_store/global_store.dart b/example/lib/global_store/global_store.dart new file mode 100644 index 0000000..2f7475c --- /dev/null +++ b/example/lib/global_store/global_store.dart @@ -0,0 +1,16 @@ +import 'package:fish_redux/fish_redux.dart'; +import 'package:sample/global_store/reducer.dart'; +import 'package:sample/global_store/state.dart'; + +class GlobalStore{ + static Store _globalStore; + static GlobalState get state => store.getState(); + + static Store get store{ + if(null == _globalStore){ + _globalStore = createStore(initState(null), buildReducer()); + } + return _globalStore; + } + +} \ No newline at end of file diff --git a/example/lib/global_store/reducer.dart b/example/lib/global_store/reducer.dart new file mode 100644 index 0000000..633b7be --- /dev/null +++ b/example/lib/global_store/reducer.dart @@ -0,0 +1,26 @@ +import 'dart:ui'; + +import 'package:fish_redux/fish_redux.dart'; + +import 'action.dart'; +import 'state.dart'; + +Reducer buildReducer() { + return asReducer( + >{ + GlobalAction.action: _onAction, + GlobalAction.changeThemeColor: _onchangeThemeColor, + }, + ); +} + +GlobalState _onAction(GlobalState state, Action action) { + final GlobalState newState = state.clone(); + return newState; +} + +GlobalState _onchangeThemeColor(GlobalState state, Action action) { + final GlobalState newState = state.clone(); + newState.themeColor = action.payload as Color; + return newState; +} diff --git a/example/lib/global_store/state.dart b/example/lib/global_store/state.dart new file mode 100644 index 0000000..f23fd08 --- /dev/null +++ b/example/lib/global_store/state.dart @@ -0,0 +1,15 @@ +import 'dart:ui'; + +import 'package:fish_redux/fish_redux.dart'; + +class GlobalState implements Cloneable { + Color themeColor; + @override + GlobalState clone() { + return GlobalState(); + } +} + +GlobalState initState(Map args) { + return GlobalState(); +} diff --git a/example/lib/todo_edit_page/action.dart b/example/lib/todo_edit_page/action.dart index 2b9e6ff..6dee49a 100644 --- a/example/lib/todo_edit_page/action.dart +++ b/example/lib/todo_edit_page/action.dart @@ -1,16 +1,20 @@ import 'package:fish_redux/fish_redux.dart'; -enum ToDoEditAction { update, done } +enum ToDoEditAction { update, done, changeTheme } class ToDoEditActionCreator { - static Action update(String name, String desc) { + static Action update(String name, String desc, String themeidx) { return Action( ToDoEditAction.update, - payload: {'name': name, 'desc': desc}, + payload: {'name': name, 'desc': desc, 'themeidx' : themeidx}, ); } static Action done() { return const Action(ToDoEditAction.done); } + + static Action changeTheme(){ + return Action(ToDoEditAction.changeTheme); + } } diff --git a/example/lib/todo_edit_page/effect.dart b/example/lib/todo_edit_page/effect.dart index 218e1da..708534d 100644 --- a/example/lib/todo_edit_page/effect.dart +++ b/example/lib/todo_edit_page/effect.dart @@ -1,5 +1,7 @@ import 'package:fish_redux/fish_redux.dart'; import 'package:flutter/material.dart'; +import 'package:sample/global_store/action.dart'; +import 'package:sample/global_store/global_store.dart'; import '../todo_list_page/todo_component/component.dart'; import 'action.dart'; @@ -9,21 +11,34 @@ Effect buildEffect() { return combineEffects(>{ Lifecycle.initState: _init, ToDoEditAction.done: _onDone, + ToDoEditAction.changeTheme : _onchangeTheme, }); } void _init(Action action, Context ctx) { ctx.state.nameEditController.addListener(() { ctx.dispatch( - ToDoEditActionCreator.update(ctx.state.nameEditController.text, null)); + ToDoEditActionCreator.update(ctx.state.nameEditController.text, null, null)); }); ctx.state.descEditController.addListener(() { ctx.dispatch( - ToDoEditActionCreator.update(null, ctx.state.descEditController.text)); + ToDoEditActionCreator.update(null, ctx.state.descEditController.text, null)); }); } void _onDone(Action action, Context ctx) { Navigator.of(ctx.context).pop(ctx.state.toDo); } + +void _onchangeTheme(Action action, Context ctx) { + ctx.state.themeIdx++; + if(ctx.state.themeIdx >= ctx.state.themeColorSlots.length) + { + ctx.state.themeIdx = 0; + } + //change global data + GlobalStore.store.dispatch(GlobalActionCreator.onchangeThemeColor(ctx.state.themeColorSlots[ctx.state.themeIdx])); + //notify todo edit page update data + ToDoEditActionCreator.update(null, null, ctx.state.themeIdx.toString()); +} diff --git a/example/lib/todo_edit_page/reducer.dart b/example/lib/todo_edit_page/reducer.dart index a40edd0..e097fed 100644 --- a/example/lib/todo_edit_page/reducer.dart +++ b/example/lib/todo_edit_page/reducer.dart @@ -5,7 +5,7 @@ import 'state.dart'; Reducer buildReducer() { return asReducer(>{ - ToDoEditAction.update: _update, + ToDoEditAction.update: _update }); } @@ -14,5 +14,7 @@ TodoEditState _update(TodoEditState state, Action action) { final TodoEditState newState = state.clone(); newState.toDo.title = update['name'] ?? newState.toDo.title; newState.toDo.desc = update['desc'] ?? newState.toDo.desc; + String strThemeIdx = update['themeidx'] ?? newState.themeIdx.toString(); + newState.themeIdx = int.tryParse(strThemeIdx); return newState; -} +} \ No newline at end of file diff --git a/example/lib/todo_edit_page/state.dart b/example/lib/todo_edit_page/state.dart index 1a1ba88..3830a76 100644 --- a/example/lib/todo_edit_page/state.dart +++ b/example/lib/todo_edit_page/state.dart @@ -11,6 +11,9 @@ class TodoEditState implements Cloneable { FocusNode focusNodeName; FocusNode focusNodeDesc; + int themeIdx; + List themeColorSlots; + @override TodoEditState clone() { return TodoEditState() @@ -18,7 +21,9 @@ class TodoEditState implements Cloneable { ..descEditController = descEditController .. focusNodeName = focusNodeName .. focusNodeDesc = focusNodeDesc - ..toDo = toDo; + ..toDo = toDo + ..themeIdx = themeIdx + ..themeColorSlots = themeColorSlots; } } @@ -29,5 +34,7 @@ TodoEditState initState(ToDoState arg) { state.descEditController = TextEditingController(text: arg?.desc); state.focusNodeName = FocusNode(); state.focusNodeDesc = FocusNode(); + state.themeIdx = 0; + state.themeColorSlots = [Colors.green, Colors.red, Colors.black, Colors.blue]; return state; } diff --git a/example/lib/todo_edit_page/view.dart b/example/lib/todo_edit_page/view.dart index 3fc4047..0d38c06 100644 --- a/example/lib/todo_edit_page/view.dart +++ b/example/lib/todo_edit_page/view.dart @@ -1,5 +1,6 @@ import 'package:fish_redux/fish_redux.dart'; import 'package:flutter/material.dart'; +import 'package:sample/global_store/global_store.dart'; import 'action.dart'; import 'state.dart'; @@ -8,6 +9,7 @@ Widget buildView( TodoEditState state, Dispatch dispatch, ViewService viewService) { return Scaffold( appBar: AppBar( + backgroundColor:GlobalStore.state.themeColor, title: const Text('Todo'), ), body: Container( @@ -40,6 +42,13 @@ Widget buildView( ], ), ), + new RaisedButton( + padding: new EdgeInsets.only(left: 20.0, top: 10.0, right: 20.0, bottom: 10.0), + color: Colors.blue, + child: new Text("Change theme", style: new TextStyle(fontSize: 18), overflow:TextOverflow.ellipsis), + onPressed: () { + dispatch(ToDoEditActionCreator.changeTheme()); + }), Expanded( child: Container( margin: const EdgeInsets.only(top: 32.0), diff --git a/example/lib/todo_list_page/view.dart b/example/lib/todo_list_page/view.dart index c431219..1136722 100644 --- a/example/lib/todo_list_page/view.dart +++ b/example/lib/todo_list_page/view.dart @@ -1,5 +1,6 @@ import 'package:fish_redux/fish_redux.dart'; import 'package:flutter/material.dart'; +import 'package:sample/global_store/global_store.dart'; import 'action.dart'; import 'state.dart'; @@ -8,6 +9,7 @@ Widget buildView(PageState state, Dispatch dispatch, ViewService viewService) { final ListAdapter adapter = viewService.buildAdapter(); return Scaffold( appBar: AppBar( + backgroundColor:GlobalStore.state.themeColor, title: const Text('ToDoList'), ), body: Container(