一,概述 

   flutter一个重要的特性就是组件化。组件分为两种状态,一种是StatefulWidget有状态组件,一种是StatelessWidget无状态组件。 无状态组件不能更新状态,有状态组件具有类似刷新的机制,可更改状态。
  功能模块都可以通过继承两种状态组件实现功能模块封装。组件间通信,一般存在一下两种关系。

      • 父子组件通信
      • 兄弟组件通信       

二, 通信实现方式

  • 回调通信

    • 需求“点击子组件,修改父组件的背景颜色与子组件背景颜色一致”
    • 代码实现
      //父组件
      
      class ParentWidget extends StatefulWidget {
      final String title;
      ParentWidget({Key key,this.title}):super(key:key); @override
      State<StatefulWidget> createState() {
      return new ParentWidgetState();
      }
      } class ParentWidgetState extends State<ParentWidget> {
      Color containerBg = Colors.orange;
      //回调函数
      void changeBackgroundColor(Color newColor){
      setState(() {
      containerBg = newColor;//修改状态
      });
      } @override
      Widget build(BuildContext context) {
      return new Scaffold(
      appBar: new AppBar(
      title: new Text(widget.title),
      ),
      body: new Center(
      child: new GestureDetector(
      onTap: (){
      changeBackgroundColor(Colors.orange);
      },
      child: new Container(
      width: 300,
      height: 300,
      color: containerBg,
      alignment: Alignment.center,
      child: new Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
      new childrenA(childrenACallBack: changeBackgroundColor),
      new childrenB(childrenBCallBack: changeBackgroundColor),
      ],
      ),
      ),
      )
      ),
      );
      }
      } //子组件(组件A)
      class childrenA extends StatelessWidget {
      //定义接收父类回调函数的指针
      final ValueChanged<Color> childrenACallBack;
      childrenA({Key key,this.childrenACallBack}):super(key:key);
        @override
      Widget build(BuildContext context) {
      return new GestureDetector(
      onTap: (){
         //调用回调函数传值
      childrenACallBack(Colors.green);
      },
      child: new Container(
      width: 80,
      height: 80,
      color: Colors.green,
      child: new Text('ChildrenA'),
      ),
      );
      }
      } //子组件(组件B)
      class childrenB extends StatelessWidget {
      final ValueChanged<Color> childrenBCallBack;
      childrenB({Key key,this.childrenBCallBack}):super(key:key); @override
      Widget build(BuildContext context) {
      return new GestureDetector(
      onTap:(){
      childrenBCallBack(Colors.red);
      },
      child: new Container(
      width: 80,
      height: 80,
      color: Colors.red,
      child: new Text('ChildredB'),
      ),
      );
      }
      }
    • 功能实现

    • 使用场景:一般用于子组件对父组件传值。
  • InheritedWidget 数据共享

    • 场景:业务开发中经常会碰到这样的情况,多个Widget需要同步同一份全局数据,比如点赞数、评论数、夜间模式等等。
    • 代码实现:
      import 'package:flutter/material.dart';
      
      void main() => runApp(MyApp());
      
      class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
      return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
      primarySwatch: Colors.blue, ),
      home: new InheritedWidgetTestContainer(),
      );
      }
      } //模型数据
      class InheritedTestModel {
      final int count;
      const InheritedTestModel(this.count);
      } //哨所(自定义InheritedWidget类)
      class InheritedContext extends InheritedWidget {
      //构造函数
      InheritedContext({
      Key key,
      @required this.inheritedTestModel,
      @required this.increment,
      @required this.reduce,
      @required Widget child
      }):super(key:key,child:child); //变量
      final InheritedTestModel inheritedTestModel;
      final Function() increment;
      final Function() reduce; //静态方法
      static InheritedContext of(BuildContext context){
      InheritedContext contexts = context.inheritFromWidgetOfExactType(InheritedContext);
      return context.inheritFromWidgetOfExactType(InheritedContext);
      }
      //是否重建取决于Widget组件是否相同
      @override
      bool updateShouldNotify(InheritedContext oldWidget) {
      return inheritedTestModel != oldWidget.inheritedTestModel;
      }
      } class TestWidgetA extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
      final inheritedContext = InheritedContext.of(context);
      return new Padding(
      padding: const EdgeInsets.only(left: 10.0,top: 10.0,right: 10.0),
      child: new RaisedButton(
      textColor: Colors.black,
      child: new Text('+'),
      onPressed:inheritedContext.increment
      ),
      );
      }
      } class TestWidgetB extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
      final inheritedContext = InheritedContext.of(context);
      return new Padding(
      padding: const EdgeInsets.only(left: 10,top: 10,right: 10.0),
      child: new RaisedButton(
      textColor: Colors.black,
      child: new Text('-'),
      onPressed: inheritedContext.reduce
      ),
      );
      }
      } class TestWidgetC extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
      final inheritedContext = InheritedContext.of(context);
      final inheritedTestModel = inheritedContext.inheritedTestModel; return new Padding(
      padding: const EdgeInsets.only(left: 10.0,top: 10.0,right: 10.0),
      child: new RaisedButton(
      textColor: Colors.black,
      child: new Text('${inheritedTestModel.count}'),
      onPressed: (){ },
      ),
      );
      }
      } class InheritedWidgetTestContainer extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
      return new InheritedWidgetTestContainerState();
      }
      } class InheritedWidgetTestContainerState extends State<InheritedWidgetTestContainer> { InheritedTestModel _inheritedTestModel; _initData(){
      _inheritedTestModel = new InheritedTestModel(0);
      } @override
      void initState() {
      _initData();
      super.initState();
      } _incrementCount(){
      setState(() {
      _inheritedTestModel = new InheritedTestModel(_inheritedTestModel.count + 1);
      });
      } _reduceCount(){
      setState(() {
      _inheritedTestModel = new InheritedTestModel(_inheritedTestModel.count - 1);
      });
      } @override
      Widget build(BuildContext context) {
      return new InheritedContext(
      inheritedTestModel: _inheritedTestModel,
      increment: _incrementCount,
      reduce: _reduceCount,
      child: new Scaffold(
      appBar: new AppBar(
      title: new Text('InheritedWidgetTest'),
      ),
      body: new Center(
      child: new Column(
      children: <Widget>[
      new TestWidgetA(),
      new TestWidgetB(),
      new TestWidgetC(),
      ],
      ),
      )
      ),
      );
      }
      }
    • 功能实现
       
    • 使用场景
      一般用于父组件对子组件的跨组件传值。

  • Global Key通信
    GlobalKey能够跨Widget访问状态。
    • 需求“点击A子组件,修改B子组件的背景颜色为指定的‘蓝色”
    • 代码实现
      //父组件
      class ParentWidget extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
      return new ParentWidgetState();
      }
      } class ParentWidgetState extends State<ParentWidget> {
      @override
      Widget build(BuildContext context) { return new Scaffold(
      appBar: new AppBar(
      title: new Text('组件化'),
      ),
      body: new Center(
      child: new Container(
      color: Colors.grey,
      width: 200,
      height: 200,
      child: new Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
      new SubWidgetA(key: subAkey),
      new SubWidgetB(key: subBkey)
      ],
      ),
      ),
      ),
      );
      }
      } //子组件A class SubWidgetA extends StatefulWidget {
      SubWidgetA({Key key}):super(key:key);
      @override
      State<StatefulWidget> createState() {
      return new SubWidgetAState();
      }
      } class SubWidgetAState extends State <SubWidgetA> { Color _backgroundColors = Colors.red;//红色
      void updateBackGroundColors(Color colos){
      setState(() {
      _backgroundColors = colos;
      });
      } @override
      Widget build(BuildContext context) {
      return new GestureDetector(
      onTap: (){
      subBkey.currentState.updateBackGroundColors(Colors.blue);
      setState(() {
      _backgroundColors = Colors.red;
      });
      },
      child: new Container(
      width: 80,
      height: 80,
      color:_backgroundColors,
      alignment: Alignment.center,
      child: new Text('SubWidgetA'),
      ),
      );
      }
      } //子组件B
      class SubWidgetB extends StatefulWidget {
      SubWidgetB({Key key}):super(key:key);
      @override
      State<StatefulWidget> createState() {
      // TODO: implement createState
      return new SubWidgetBState();
      }
      } class SubWidgetBState extends State<SubWidgetB> { Color _backgroundColors = Colors.green;//绿色
      void updateBackGroundColors(Color colos){
      setState(() {
      _backgroundColors = colos;
      });
      } @override
      Widget build(BuildContext context) {
      return new GestureDetector(
      onTap: (){
      subAkey.currentState.updateBackGroundColors(Colors.blue);
      setState(() {
      _backgroundColors = Colors.green;
      }); },
      child: new Container(
      width: 80,
      height: 80,
      color: _backgroundColors,
      alignment: Alignment.center,
      child: new Text('SubWidgetB'),
      ),
      );
      }
      }
    • 功能实现
    • 使用场景:一般用于跨组件访问状态
  • ValueNotifier通信 

     ValueNotifier是一个包含单个值的变更通知器,当它的值改变的时候,会通知它的监听。

    1. 定义ValueNotifierData类,继承ValueNotifier

      class ValueNotifierData extends ValueNotifier<String> {
      ValueNotifierData(value) : super(value);
      }
    1. 定义_WidgetOne,包含一个ValueNotifierData的实例。

      class _WidgetOne extends StatefulWidget {
      _WidgetOne({this.data});
      final ValueNotifierData data;
      @override
      _WidgetOneState createState() => _WidgetOneState();
      }
    1. _WidgetOneState中给ValueNotifierData实例添加监听。

      @override
      initState() {
      super.initState();
      widget.data.addListener(_handleValueChanged);
      info = 'Initial mesage: ' + widget.data.value;
      } void _handleValueChanged() {
      setState(() {
      info = 'Message changed to: ' + widget.data.value;
      });
    1. ValueNotifierCommunication组件中实例化_WidgetOne,可以通过改变ValueNotifierData实例的value来触发_WidgetOneState的更新。

      @override
      Widget build(BuildContext context) {
      ValueNotifierData vd = ValueNotifierData('Hello World');
      return Scaffold(
      appBar: AppBar(title: Text('Value Notifier Communication'),),
      body: _WidgetOne(data: vd),
      floatingActionButton: FloatingActionButton(child: Icon(Icons.refresh),onPressed: () {
      vd.value = 'Yes';
      }),
      );
      }
  • 第三方插件
      在这里运用event_bus来实现传值,用于组件与组件之间的传值。
    • event_bus 

      • 引入插件

        import 'package:event_bus/event_bus.dart';
      • event_bus用法。

        • 新建消息监测类

          import 'package:event_bus/event_bus.dart';
          EventBus eventBus = new EventBus();
          class TransEvent{
          String text;
          TransEvent(this.text);
          }
        • 监测类变化

          eventBus.on<TransEvent>().listen((TransEvent data) => show(data.text));
          void show(String val) {
          setState(() {
          data = val;
          });
          }
        • 触发消息变化

          eventBus.fire(new TransEvent('$inputText'));
      • 使用场景:这样我们就可以根据这些来实现组件之间的传值。

【Flutter学习】组件通信(父子、兄弟)的更多相关文章

  1. react第六单元(react组件通信-父子组件通信-子父组件通信-跨级组件的传参方式-context方式的传参)

    第六单元(react组件通信-父子组件通信-子父组件通信-跨级组件的传参方式-context方式的传参) #课程目标 1.梳理react组件之间的关系 2.掌握父子传值的方法 3.掌握子父传值的方法 ...

  2. vue组件之间的通信, 父子组件通信,兄弟组件通信

    组件通讯包括:父子组件间的通信和兄弟组件间的通信.在组件化系统构建中,组件间通信必不可少的. 父组件--> 子组件 1. 属性设置 父组件关键代码如下: <template> < ...

  3. vue组件通信&&v兄弟组件通信eventbus遇到的问题(多次触发、第一次不触发)

    组件通讯包括:父子组件间的通信和兄弟组件间的通信.在组件化系统构建中,组件间通信必不可少的 (vuex以后再说). 父组件--> 子组件 1. 属性设置 父组件关键代码如下: <templ ...

  4. 计算属性、侦听属性、局部与全局组件使用、组件通信(父子互传)、ref属性、动态组件和keep-alive、插槽

    今日内容概要 计算属性 侦听属性 局部组件和全局组件 组件通信之父传子 组件通信之子传父 ref属性(组件间通信) 动态组件和keep-alive 插槽 内容详细 1.计算属性 # 插值的普通函数,只 ...

  5. vue组件详解——组件通信

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 组件之间通信可以用下图表示: 组件关系可分为父子组件通信.兄弟组件通信.跨级组件通信. ...

  6. vue组件详解(三)——组件通信

    组件之间通信可以用下图表示: 组件关系可分为父子组件通信.兄弟组件通信.跨级组件通信. 一.自定义事件 当子组件需要向父组件传递数据时,就要用到自定义事件. 子组件用$emit ()来触发事件,父组件 ...

  7. vue2.0 $emit $on组件通信

    在vue1.0中父子组件通信使用$dispatch 和 $broadcast,但是在vue2.0中$dispatch 和 $broadcast 已经被弃用. 因为基于组件树结构的事件流方式实在是让人难 ...

  8. vue 2 使用Bus.js进行兄弟(非父子)组件通信 简单案例

    vue2中废弃了$dispatch和$broadcast广播和分发事件的方法.父子组件中可以用props和$emit().如何实现非父子组件间的通信,可以通过实例一个vue实例Bus作为媒介,要相互通 ...

  9. vue2.0 父子组件通信 兄弟组件通信

    父组件是通过props属性给子组件通信的来看下代码: 父组件: <parent> <child :child-com="content"></chil ...

随机推荐

  1. LINUX时间服务器搭建

    一. 因 为工作需要,偶需要将搭建一个NTP服务器来进行时间同步的测试,在公司里一直以为非常的难搭建,也是刚刚工作的缘故,就等正导师给帮着弄一台服务器,结 果导师给了我一个系统叫Fedora,让我偶自 ...

  2. codeforces gym100418J

    题目简述 给定N 求1到N中有多少个幸运数字 幸运数字的定义为 这个数能被它二进制表示下1的个数整除 其中(1 ≤ N ≤ 1019) -------------------------------- ...

  3. vim中 E212:无法打开并写入文件 的解决办法

    因为centos7刚安装的时候是无法上网的,所以,需要去配置一下ifcfg-ens33文件,但实际上这个文件是只读的文件,root也无法去更改内容,这时候保存的时候需要使用 w ! sudo tee ...

  4. 用DECODE进行排序

    DECODE用法: 现定义一table名为output,其中定义两个column分别为monthid(var型)和sale(number型),若sale值=1000时翻译为D,=2000时翻译为C,= ...

  5. 用 Flask 来写个轻博客 (25) — 使用 Flask-Principal 实现角色权限功能

    目录 目录 前文列表 扩展阅读 Flask-Principal 使用 Flask-Principal 来实现角色权限功能 添加 Role Model 在 Manager shell 中手动的添加角色 ...

  6. Zlib not installed

    若提示:zlib not installed   wget http://zlib.net/zlib-1.2.8.tar.gz   tar zxf zlib-1.2.8.tar.gz cd zlib- ...

  7. java方法调用及传参

    静态方法:有static修饰的方法. 非静态方法:没有static修饰的方法. 方法调用: 一静态方法调用 静态方法/属性 1)一个类:直接调用. 2)不同类/不同文件: a: 类名.属性名/方法名 ...

  8. CF1241 D Sequence Sorting(离散化+DP)

    题意: 给定数组a[n],用两种操作: 1.将数组中所有值为x的数移至开头 2.将数组中所有值为x的数移至末尾 问,经过最少多少次操作,能将数组a[n]变为非递减的有序数列? (1<=n< ...

  9. PAT甲级【2019年9月考题】——A1160 Forever【20】

    7-1 Forever (20 分) "Forever number" is a positive integer A with K digits, satisfying the ...

  10. BZOJ 4765(分块+树状数组)

    题面 传送门 "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能计算数列区间和,而普通计算姬能计算树中子树和.更 ...