Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:rx_redux/rx_redux.dart';
  3. import 'package:rxdart/rxdart.dart';
  4. import 'package:random_string/random_string.dart';
  5.  
  6. void main() => runApp(MyApp());
  7.  
  8. class MyApp extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. title: 'Flutter Demo',
  13. theme: ThemeData(primarySwatch: Colors.red),
  14. home: MyHomePage(),
  15. );
  16. }
  17. }
  18.  
  19. class MyHomePage extends StatefulWidget {
  20. const MyHomePage({Key key}) : super(key: key);
  21.  
  22. @override
  23. _MyHomePageState createState() => _MyHomePageState();
  24. }
  25.  
  26. // actions
  27.  
  28. abstract class Action {}
  29.  
  30. class AddTodo implements Action {
  31. final Todo todo;
  32.  
  33. AddTodo(this.todo);
  34. }
  35.  
  36. class RemoveTodo implements Action {
  37. final Todo todo;
  38.  
  39. RemoveTodo(this.todo);
  40. }
  41.  
  42. class ToggleTodo implements Action {
  43. final Todo todo;
  44.  
  45. ToggleTodo(this.todo);
  46. }
  47.  
  48. class TodoAdded implements Action {
  49. final Todo todo;
  50.  
  51. TodoAdded(this.todo);
  52. }
  53.  
  54. class TodoRemoved implements Action {
  55. final Todo todo;
  56.  
  57. TodoRemoved(this.todo);
  58. }
  59.  
  60. class TodoToggled implements Action {
  61. final Todo todo;
  62.  
  63. TodoToggled(this.todo);
  64. }
  65.  
  66. // view state
  67.  
  68. class Todo {
  69. final int id;
  70. final String title;
  71. final bool completed;
  72.  
  73. const Todo(this.id, this.title, this.completed);
  74. }
  75.  
  76. class ViewState {
  77. final List<Todo> todos;
  78.  
  79. const ViewState(this.todos);
  80. }
  81.  
  82. class _MyHomePageState extends State<MyHomePage> {
  83. static var _id = 0;
  84. final actionS = PublishSubject<Action>();
  85. ValueObservable<ViewState> state$;
  86.  
  87. @override
  88. void initState() {
  89. super.initState();
  90.  
  91. const initialVS = ViewState([]);
  92. state$ = actionS
  93. .transform(
  94. ReduxStoreStreamTransformer<Action, ViewState>(
  95. initialStateSupplier: () => initialVS,
  96. reducer: (ViewState vs, Action action) {
  97. if (action is AddTodo) return vs;
  98. if (action is RemoveTodo) return vs;
  99. if (action is ToggleTodo) return vs;
  100.  
  101. if (action is TodoAdded) {
  102. return ViewState([...vs.todos, action.todo]);
  103. }
  104.  
  105. if (action is TodoRemoved) {
  106. return ViewState(
  107. vs.todos.where((t) => t.id != action.todo.id).toList(),
  108. );
  109. }
  110.  
  111. if (action is TodoToggled) {
  112. return ViewState(
  113. vs.todos.map((t) {
  114. if (t.id != action.todo.id) {
  115. return t;
  116. } else {
  117. return Todo(
  118. t.id,
  119. t.title,
  120. !t.completed,
  121. );
  122. }
  123. }).toList(),
  124. );
  125. }
  126.  
  127. return vs;
  128. },
  129. sideEffects: [
  130. (action$, state) {
  131. return action$
  132. .whereType<AddTodo>()
  133. .map((action) => action.todo)
  134. .flatMap((todo) async* {
  135. await Future.delayed(const Duration(milliseconds: 500));
  136. yield TodoAdded(todo);
  137. });
  138. },
  139. (action$, state) {
  140. return action$
  141. .whereType<RemoveTodo>()
  142. .map((action) => action.todo)
  143. .flatMap((todo) async* {
  144. await Future.delayed(const Duration(milliseconds: 500));
  145. yield TodoRemoved(todo);
  146. });
  147. },
  148. (action$, state) {
  149. return action$
  150. .whereType<ToggleTodo>()
  151. .map((action) => action.todo)
  152. .flatMap((todo) async* {
  153. await Future.delayed(const Duration(milliseconds: 500));
  154. yield TodoToggled(todo);
  155. });
  156. },
  157. ],
  158. ),
  159. )
  160. .shareValueSeeded(initialVS);
  161. }
  162.  
  163. @override
  164. void dispose() {
  165. super.dispose();
  166. }
  167.  
  168. @override
  169. Widget build(BuildContext context) {
  170. return Scaffold(
  171. appBar: AppBar(
  172. title: Text('RxRedux demo'),
  173. ),
  174. body: StreamBuilder<ViewState>(
  175. initialData: state$.value,
  176. stream: state$,
  177. builder: (context, snapshot) {
  178. final todos = snapshot.data.todos;
  179.  
  180. return ListView.builder(
  181. itemCount: todos.length,
  182. itemBuilder: (context, index) {
  183. final todo = todos[index];
  184.  
  185. return CheckboxListTile(
  186. title: Text(todo.title),
  187. onChanged: (_) => actionS.add(ToggleTodo(todo)),
  188. value: todo.completed,
  189. secondary: IconButton(
  190. icon: Icon(
  191. Icons.delete,
  192. color: Theme.of(context).accentColor,
  193. ),
  194. onPressed: () => actionS.add(RemoveTodo(todo)),
  195. ),
  196. );
  197. },
  198. );
  199. },
  200. ),
  201. floatingActionButton: FloatingActionButton(
  202. child: Icon(Icons.add),
  203. onPressed: () {
  204. actionS.add(
  205. AddTodo(
  206. Todo(
  207. ++_id,
  208. randomString(10),
  209. false,
  210. ),
  211. ),
  212. );
  213. },
  214. ),
  215. );
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement