Flutter fish-redux

2022-09-01  本文已影响0人  doudo

在写代码前,先看写下流程图

image 20210630174342971.png

可以发现,事件的传递,都是通过dispatch这个方法,而且action这层很明显是非常关键的一层,事件的传递,都是在该层定义和中转的

工程文件说明

计数器demo

具体的demo可以展示:

这个例子演示,view中点击此操作,然后更新页面数据。下述的流程,在effect中把数据处理好,通过action中转传递给reducer更新数据
view —> action —> effect —> reducer(更新数据)
注意:该流程将展示,怎么将数据在各流程中互相传递

page

import 'package:fish_redux/fish_redux.dart';

import 'effect.dart';
import 'reducer.dart';
import 'state.dart';
import 'view.dart';

class OaBaseInfoPage extends Page<OaBaseInfoState, Map<String, dynamic>> {
  OaBaseInfoPage()
      : super(
            initState: initBaseInfoState,
            effect: buildEffect(),
            reducer: buildReducer(),
            view: buildView,
            dependencies: Dependencies<OaBaseInfoState>(
                adapter: null,
                slots: <String, Dependent<OaBaseInfoState>>{
                }),
            middleware: <Middleware<OaBaseInfoState>>[
            ],);

state

import 'package:fish_redux/fish_redux.dart';

class CountState implements Cloneable<CountState> {
  int count;

  @override
  CountState clone() {
    return CountState()..count = count;
  }
}

CountState initState(Map<String, dynamic> args) {
  return CountState()..count = 0;
}

view

view:这里面就是写界面的模块,buildView里面有三个参数

import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(CountState state, Dispatch dispatch, ViewService viewService) {
  return _bodyWidget(state,dispatch);
}

Widget _bodyWidget(CountState state, Dispatch dispatch) {
  return Scaffold(
    appBar: AppBar(
      title: Text("FishRedux"),
    ),
    body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text('You have pushed the button this many times:'),
          Text(state.count.toString()),
        ],
      ),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: () {
        ///点击事件,调用action 计数自增方法
        dispatch(CountActionCreator.countIncrease());
      },
      child: Icon(Icons.add),
    ),
  );
}

action

该层是非常重要的模块,页面所有的行为都可以在本层直观的看到

import 'package:fish_redux/fish_redux.dart';

//TODO replace with your own action
enum CountAction { increase,updateCount }

class CountActionCreator {

  ///去effect层去处理自增数据
  static Action countIncrease() {
    return Action(CountAction.increase);
  }
  ///去reducer层更新数据,传参可以放在Action类中的payload字段中,payload是dynamic类型,可传任何类型
  static Action updateCount(int count) {
    return Action(CountAction.updateCount, payload: count);
  }
}

effect

import 'package:fish_redux/fish_redux.dart';
import 'action.dart';
import 'state.dart';

Effect<CountState> buildEffect() {
  return combineEffects(<Object, Effect<CountState>>{
    CountAction.increase: _onIncrease,
  });
}

///自增数
void _onIncrease(Action action, Context<CountState> ctx) {
  ///处理自增数逻辑
  int count = ctx.state.count + 1;
  ctx.dispatch(CountActionCreator.updateCount(count));
}

reducer

import 'package:fish_redux/fish_redux.dart';

import 'action.dart';
import 'state.dart';

Reducer<CountState> buildReducer() {
  return asReducer(
    <Object, Reducer<CountState>>{
      CountAction.updateCount: _updateCount,
    },
  );
}

CountState _updateCount(CountState state, Action action) {
  final CountState newState = state.clone();
  newState.count = action.payload;
  return newState;
}

参考:
Flutter fish-redux 简单使用
手把手入门Fish-Redux开发flutter
Flutter FishRedux使用步骤

上一篇 下一篇

猜你喜欢

热点阅读