Flutter全局状态管理机

需求:
· 在我的界面,展示了用户信息姓名、年龄、性别等信息
· 我的界面有一个设置按钮,可以修改这些用户信息
· 修改之后怎么刷新呢?
· 这时候就使用到全局状态管理

dependencies:
  flutter:
    sdk: flutter

  flutter_redux: ^0.5.2  #全局状态管理机

eg:

// 再添加其他的全局类,都在初始化store时候添加进去
// YYRedux.dart

import 'package:ly_app/Model/UserInfo.dart'; // UserInfo类
import 'package:ly_app/Redux/UserInfoRedux.dart'; //UserInfoRedux类

// 需要全局的对象在YYState类里面添加
class YYState {
  UserInfo userInfo;

  YYState({this.userInfo});
}

// 创建store使用
YYState appReducer(YYState state, action) {
  return YYState(
    // 将全局对象和action关联
    userInfo: UserInfoReducer(state.userInfo, action),
  );
}
// UserInfoReducer.dart

import 'package:ly_app/Model/UserInfo.dart';
import 'package:redux/redux.dart'; // redux全局状态管理

/// redux 的 combineReducers, 通过 TypedReducer 将 UpdateUserInfoAction 与 reducers 关联起来
final UserInfoReducer = combineReducers<UserInfo>([
  TypedReducer<UserInfo, UpdateUserInfoAction>(_updateLoaded),
]);

/// 如果有 UpdateUserAction 发起一个请求时
/// 就会调用到 _updateLoaded
/// _updateLoaded 这里接受一个新的userInfo,并返回
/// 更新全局对象时候调用的方法
UserInfo _updateLoaded(UserInfo userInfo, action) {
  userInfo = action.userInfo;
  return userInfo;
}

class UpdateUserInfoAction {
  final UserInfo userInfo;
  UpdateUserInfoAction(this.userInfo);
}

// main.dart

import 'package:flutter/material.dart';

import 'package:flutter/cupertino.dart';
import 'package:ly_app/MainPage.dart';

import 'package:ly_app/Model/UserInfo.dart';

import 'package:redux/redux.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:ly_app/Redux/YYRedux.dart';

void main() {
  runApp(
    new Rooter()
  );
}

class Rooter extends StatelessWidget {
/// 在app入口 初始化全局状态管理对象store
/// StoreProvider 接收对象store
  final store = new Store<YYState>(appReducer, initialState: new YYState(userInfo: UserInfo.empty()));

  Rooter({Key key}) : super(key : key);

  @override
  Widget build(BuildContext context) {
    return new StoreProvider(
        store: store,
        child: new StoreBuilder<YYState>(builder: (context, store) {
            return new MainPage();
          }
        )
    );
  }
}

访问数据⏬

    StoreProvider.of(context).state.userInfo;

更新数据⏬

    StoreProvider.of(context).dispatch(new UpdateUserInfoAction(newUserInfo));

/// 在需要更新或者获取全局状态时候需要获取到store ⏬

Store<YYState> _getStore() {
  if (context == null) {
    print("YYState null");
    return null;
  }
  return StoreProvider.of(context);
}

/// 点击事件 更新全局状态 1、创建对象 2、调用更新方法⏬

onPressed: () {
  var map={"name":"更爱","id":2};
  UserInfo newUserInfo = UserInfo.fromJson(map);
  _getStore()?.dispatch(new UpdateUserInfoAction(newUserInfo));
}

/// 哪里需要使用全局状态,就在最外面new StoreBuilder⏬

@override
Widget build(BuildContext context) {
  return new StoreBuilder<YYState>(builder: (context, store) {
    return new Text(_getStore().state.userInfo.name)
  });

Flutter完整开发实战详解
Dio网络请求
UI界面

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,140评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,444评论 2 59
  • 2018年3月11日 亲子日记第十五篇 晚上半夜一点多一直到今天中午可把我折腾...
    暖洋洋的春天阅读 230评论 0 2
  • 2017年8月20日 星期天 多云转晴 今天, 原本约好和朋友一起带孩子一去玩,临时有事不能去了,...
    千雨轩阅读 178评论 0 4
  • 近几年,移动网络游戏呈现出爆炸式增长态势,行业洗牌的速度也同样令人惊叹!在生存成本与买方盈利需求的双重压力下,许多...
    Linda玲儿阅读 817评论 0 1

友情链接更多精彩内容