Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:rx_redux/rx_redux.dart';
- import 'package:rxdart/rxdart.dart';
- import 'package:random_string/random_string.dart';
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(primarySwatch: Colors.red),
- home: MyHomePage(),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- const MyHomePage({Key key}) : super(key: key);
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- // actions
- abstract class Action {}
- class AddTodo implements Action {
- final Todo todo;
- AddTodo(this.todo);
- }
- class RemoveTodo implements Action {
- final Todo todo;
- RemoveTodo(this.todo);
- }
- class ToggleTodo implements Action {
- final Todo todo;
- ToggleTodo(this.todo);
- }
- class TodoAdded implements Action {
- final Todo todo;
- TodoAdded(this.todo);
- }
- class TodoRemoved implements Action {
- final Todo todo;
- TodoRemoved(this.todo);
- }
- class TodoToggled implements Action {
- final Todo todo;
- TodoToggled(this.todo);
- }
- // view state
- class Todo {
- final int id;
- final String title;
- final bool completed;
- const Todo(this.id, this.title, this.completed);
- }
- class ViewState {
- final List<Todo> todos;
- const ViewState(this.todos);
- }
- class _MyHomePageState extends State<MyHomePage> {
- static var _id = 0;
- final actionS = PublishSubject<Action>();
- ValueObservable<ViewState> state$;
- @override
- void initState() {
- super.initState();
- const initialVS = ViewState([]);
- state$ = actionS
- .transform(
- ReduxStoreStreamTransformer<Action, ViewState>(
- initialStateSupplier: () => initialVS,
- reducer: (ViewState vs, Action action) {
- if (action is AddTodo) return vs;
- if (action is RemoveTodo) return vs;
- if (action is ToggleTodo) return vs;
- if (action is TodoAdded) {
- return ViewState([...vs.todos, action.todo]);
- }
- if (action is TodoRemoved) {
- return ViewState(
- vs.todos.where((t) => t.id != action.todo.id).toList(),
- );
- }
- if (action is TodoToggled) {
- return ViewState(
- vs.todos.map((t) {
- if (t.id != action.todo.id) {
- return t;
- } else {
- return Todo(
- t.id,
- t.title,
- !t.completed,
- );
- }
- }).toList(),
- );
- }
- return vs;
- },
- sideEffects: [
- (action$, state) {
- return action$
- .whereType<AddTodo>()
- .map((action) => action.todo)
- .flatMap((todo) async* {
- await Future.delayed(const Duration(milliseconds: 500));
- yield TodoAdded(todo);
- });
- },
- (action$, state) {
- return action$
- .whereType<RemoveTodo>()
- .map((action) => action.todo)
- .flatMap((todo) async* {
- await Future.delayed(const Duration(milliseconds: 500));
- yield TodoRemoved(todo);
- });
- },
- (action$, state) {
- return action$
- .whereType<ToggleTodo>()
- .map((action) => action.todo)
- .flatMap((todo) async* {
- await Future.delayed(const Duration(milliseconds: 500));
- yield TodoToggled(todo);
- });
- },
- ],
- ),
- )
- .shareValueSeeded(initialVS);
- }
- @override
- void dispose() {
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('RxRedux demo'),
- ),
- body: StreamBuilder<ViewState>(
- initialData: state$.value,
- stream: state$,
- builder: (context, snapshot) {
- final todos = snapshot.data.todos;
- return ListView.builder(
- itemCount: todos.length,
- itemBuilder: (context, index) {
- final todo = todos[index];
- return CheckboxListTile(
- title: Text(todo.title),
- onChanged: (_) => actionS.add(ToggleTodo(todo)),
- value: todo.completed,
- secondary: IconButton(
- icon: Icon(
- Icons.delete,
- color: Theme.of(context).accentColor,
- ),
- onPressed: () => actionS.add(RemoveTodo(todo)),
- ),
- );
- },
- );
- },
- ),
- floatingActionButton: FloatingActionButton(
- child: Icon(Icons.add),
- onPressed: () {
- actionS.add(
- AddTodo(
- Todo(
- ++_id,
- randomString(10),
- false,
- ),
- ),
- );
- },
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement