上一篇文章,我们介绍了如何在flutter中使用redux。在上一篇文章的例子中,我们使用了单界面集成redux,但是在实际项目中,我们通常涉及多个模块,每个模块涉及多个界面,那么如何使用redux整合模块,并实现模块和界面间的消息传递呢?

例子:登录

接着上篇文章,这次的例子稍微复杂一点:

目标:

  • 实现登录界面,实现基本登录逻辑
  • 成功之后将结果通知到其他界面

先将AppState等状态有关的移动到reducers.dart,创建LoginPage.dart

LoginPage.dart代码如下:

import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter_use_redux/reducers.dart';
import 'package:redux/redux.dart';
import 'dart:async';
import 'package:flutter/cupertino.dart'; typedef Future CallLogin(String account,String pwd); class LoginPageState extends State<LoginPage>{ String _account;
String _pwd; bool isLogin; @override
void initState() {
super.initState();
} @override
void dispose() {
super.dispose();
} @override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("登录"),
),
body: new Form(
onChanged: (){
print("changed");
},
onWillPop: () async{
return true;
},
child: new Padding(padding: new EdgeInsets.all(10.0),child: new Column( children: <Widget>[
new TextFormField( decoration:new InputDecoration(labelText: "请输入账号") ,
onSaved: (String value){
_account = value;
}, ///保持一下输入的账号
validator: (String value)=> value.isEmpty ? "请输入账号" : null, ),
new TextFormField(decoration:new InputDecoration(labelText: "请输入密码"),
onSaved: (String value)=>_pwd = value, ///保持一下输入的密码
validator: (String value)=> value.isEmpty ? "请输入密码" : null),
new FormField(builder: (FormFieldState s){
return new Center(
child: new RaisedButton(onPressed: () async{ FormState state = Form.of(s.context);
if(state.validate()){
//如果验证成功,那么执行登录逻辑
state.save();
print("Login success $_account" );
//这里交给设置好的逻辑去执行操作
try{
await widget.callLogin(_account,_pwd);
showDialog(context: context,builder: (c){ return new CupertinoAlertDialog(
title: new Text("登录成功"),
actions: <Widget>[
new Center(
child: new RaisedButton(
onPressed:(){
Navigator.of(context).pop();
Navigator.of(context).pop();
},
child: new Text("ok"),
)
)
],
); });
// Navigator.of(context).pop();
}catch(e){
showDialog(context: context,builder: (c){ return new CupertinoAlertDialog(
title: new Text("登录失败$e"),
actions: <Widget>[
new Center(
child: new RaisedButton(
onPressed:(){
Navigator.of(context).pop();
},
child: new Text("ok"),
)
)
],
); });
///登录失败,提示一下用户
print("登录失败! $e");
} } },child: new Text("提交"),)
);
})
], ),)),
);
} } class LoginPage extends StatefulWidget{ CallLogin callLogin; LoginPage({this.callLogin}); @override
State<StatefulWidget> createState() {
return new LoginPageState();
} }

注意:这个组件其实并没有使用redux,登录逻辑使用外部传递过来的函数来处理:

 CallLogin callLogin;
LoginPage({this.callLogin}); ...
//执行登录逻辑
await widget.callLogin(_account,_pwd);
...

为什么要这么做呢?好处有哪些?

  • 减少组件之间的依赖关系
  • 减少本类的职责,将本类的职责变成只展示ui
  • 本类可单独单元测试,单独工作,只要传进来一个模拟的逻辑函数

那么在哪里将登录逻辑传递进来呢?

···
routes: {

    "login":(BuildContext context)=>new StoreConnector(builder: ( BuildContext context,Store<AppState> store ){

      return new LoginPage(callLogin: (String account,String pwd) async{
print("正在登录,账号$account,密码:$pwd");
// 为了模拟实际登录,这里等待一秒
await new Async.Future.delayed(new Duration(milliseconds: 1000));
if(pwd != "123456"){
throw ("登录失败,密码必须是123456");
}
print("登录成功!");
store.dispatch(new LoginSuccessAction(account: account)); },);
}, converter: (Store<AppState> store){
return store;
}),

···

在导入这个登录组件到应用程序路由上面的时候,这个时候将逻辑和Store进行对接,这样做,就将逻辑和ui彻底的分开了。

这里涉及到异步操作,那么就会遇到所谓“副作用”问题。
随着项目的增大,reducer也会越来越大,那么有什么办法可以管理呢?
这些问题我们改天再分享。

附件:

老规矩,代码在这里:
https://github.com/jzoom/flut...

如有疑问,请加qq群854192563讨论

flutter中使用redux之多界面互动的更多相关文章

  1. 理解 Flutter 中的 Key

    概览 在 Flutter 中,大概大家都知道如何更新界面视图: 通过修改 Stata 去触发 Widget 重建,触发和更新的操作是 Flutter 框架做的. 但是有时即使修改了 State,Flu ...

  2. 在React中使用Redux

    这是Webpack+React系列配置过程记录的第六篇.其他内容请参考: 第一篇:使用webpack.babel.react.antdesign配置单页面应用开发环境 第二篇:使用react-rout ...

  3. 在Flutter中嵌入Native组件的正确姿势是...

    引言 在漫长的从Native向Flutter过渡的混合工程时期,要想平滑地过渡,在Flutter中使用Native中较为完善的控件会是一个很好的选择.本文希望向大家介绍AndroidView的使用方式 ...

  4. mvp 在 flutter 中的应用

    在 Android 应用程序开发过程中,我们经常会用到一些所谓的架构方法,如:mvp,mvvm,clean等.之所以这些方法会被推崇是因为他们可以大大的解耦我们的代码的功能模块,让我们的代码在项目中后 ...

  5. Flutter 中文文档网站 flutter.cn 正式发布!

    在通常的对 Flutter 介绍中,最耳熟能详的是下面四个特点: 精美 (Beautiful):充分的赋予和发挥设计师的创造力和想象力,让你真正掌控屏幕上的每一个像素. ** 极速 (Fast)**: ...

  6. Flutter中管理路由栈的方法和应用

    原文地址:https://www.jianshu.com/p/5df089d360e4 本文首先讲的Flutter中的路由,然后主要讲下Flutter中栈管理的几种方法. 了解下Route和Navig ...

  7. 在Flutter中构建布局

    这是在Flutter中构建布局的指南.首先,您将构建以下屏幕截图的布局.然后回过头, 本指南将解释Flutter的布局方法,并说明如何在屏幕上放置一个widget.在讨论如何水平和垂直放置widget ...

  8. flutter中的生命周期函数

    前言:生命周期是一个组件加载到卸载的整个周期,熟悉生命周期可以让我们在合适的时机做该做的事情,flutter中的State生命周期和android以及React Native的生命周期类似. 先看一张 ...

  9. flutter 中的AppBar

    在flutter中的很多页面中,都会有下面这段代码: 对应就是下图中的红色线框区域,被称作AppBar顶部导航. 项目准备 在使用AppBar之前,我们先新建一个tabBar的项目: 然后在pages ...

随机推荐

  1. 【C++】VS2017 不能将const char * 分配给 char *

    我的方式是把结构体中定义的 char * 换成string #include <iostream> #include<string> using namespace std; ...

  2. Hadoop(十一):组合任务概述和格式

    组合任务概述 一些复杂的任务很难由一个MR处理完成,所以一般需要将其拆分成为多个简单的MR子任务来执行. MapReduce框架中对于这类的问题提供了几种方式进行任务执行流程的控制,主要包括以下几种方 ...

  3. problems

    exceptionUnable to connect to userservice.shanmaohuwai.com:80 . Error #0: stream_socket_client(): un ...

  4. Python 1基础语法四(数字类型、输入输出汇总和命令行参数)

    一.数字(Number)类型 python中数字有四种类型:整数.布尔型.浮点数和复数. int (整数), 如 1, 只有一种整数类型 int,表示为长整型,没有 python2 中的 Long. ...

  5. iphone se2的优缺点分析:

    4月15日晚间消息,在毫无征兆的情况下苹果公司刚刚正式发布iPhone SE二代手机,这款传闻多年的产品终于出现,国内定价人民币3299元起.本周五开始预定,4月24日开始送货. Phone SE‭‮ ...

  6. curl 交叉编译 支持http2和openssl

    touch run.sh chmod 755 run.sh mkdir build cd build ../run.sh run.sh #!/bin/bash #cd /build ../config ...

  7. 【python实现卷积神经网络】开始训练

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  8. Redis学习三:Redis高可用之哨兵模式

    申明 本文章首发自本人公众号:壹枝花算不算浪漫,如若转载请标明来源! 感兴趣的小伙伴可关注个人公众号:壹枝花算不算浪漫 22.jpg 前言 Redis 的 Sentinel 系统用于管理多个 Redi ...

  9. JUC强大的辅助类讲解--->>>CountDownLatchDemo (减少计数)

    原理: CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,这些线程会阻塞.其它线程调用countDown方法会将计数器减1(调用countDown方法的线程不会阻塞), ...

  10. stand up meeting 11/18/2015

    今日工作总结: 冯晓云:完成C#版本API的class library编译,尝试与主程序进行通信:昨天临时通知让用C++封装,不解!!![后续:我用C#做了一个查词的APP,调用的就是这个API的DL ...