『Flutter』組件通信傳值學(xué)習(xí)

flutter 組件通信學(xué)習(xí)~

父子組件正向傳值

class Parent extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return ParentState();
  }
}
class ParentState extends State<Parent> {
  String data = "無";
  String dataTwo = "傳遞給組件2的值";

  void onChanged(val){
    setState(() {
      data = val;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Center(
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          new Container(
            width: 400,
            margin: const EdgeInsets.all(10),
            padding: const EdgeInsets.only(top: 30, bottom: 50),
            decoration: BoxDecoration(color: Colors.blue[100]),
            child: new Column(
              children: <Widget>[
                new childOne(),
                new childTwo(dataTwo: dataTwo,callBack: (value)=>onChanged(value)),
                new Container(
                  padding: new EdgeInsets.only(bottom: 15),
                  child: new Text('父組件'),
                ),
                new Text('子組件2,傳過來的值' + '$data'),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

定義父組件變量 dataTwo,在子組件childTwo的構(gòu)造方法中把值穿進(jìn)去。
在childTwo中重新定義構(gòu)造方法。其中dataTwo就是從父組件傳進(jìn)來的值。

class childTwo extends StatefulWidget {
  childTwo({Key key, this.dataTwo, this.callBack}) : super(key: key);
  final callBack;
  String dataTwo;

對于dataTwo的值我們可以在 initState中通過widget.dataTwo拿到。

  @override
  void initState() {
    // TODO: implement initState
    data = widget.dataTwo;
    super.initState();
  }

父子組件間逆向傳值

其實父子組件傳值有點像vue的傳值,搞過vue的應(yīng)該挺好理解這個。
自定義構(gòu)造方法中

  void onChanged(val){
    setState(() {
      data = val;
    });
  }
 new childTwo(dataTwo: dataTwo,callBack: (value)=>onChanged(value))

在父組件提前定義好,接收到子組件調(diào)用的方法,onchanged。
在子組件中
自定義構(gòu)造方法把callBack方法傳進(jìn)來

 childTwo({Key key, this.dataTwo, this.callBack}) : super(key: key);
  final callBack;
  String dataTwo;

可以調(diào)用下面的方法進(jìn)行傳值。

widget.callBack('$inputText');

組件和組件之間的傳值

在這里運用event_bus來實現(xiàn)傳值。

import 'package:event_bus/event_bus.dart';

event_bus用法。
新建消息監(jiān)測類

import 'package:event_bus/event_bus.dart';
EventBus eventBus = new EventBus();
class TransEvent{
  String text;
  TransEvent(this.text);
}

監(jiān)測類變化

 eventBus.on<TransEvent>().listen((TransEvent data) => show(data.text));

 void show(String val) {
    setState(() {
      data = val;
    });
  }

觸發(fā)消息變化

 eventBus.fire(new TransEvent('$inputText'));

這樣我們就可以根據(jù)這些來實現(xiàn)組件之間的傳值。

https://github.com/Butteryflyyer/FlutterStudyDemo這是我學(xué)習(xí)flutter的代碼地址,平時寫的demo都會放到這里面。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 組件(Component)是Vue.js最核心的功能,也是整個架構(gòu)設(shè)計最精彩的地方,當(dāng)然也是最難掌握的。...
    六個周閱讀 5,762評論 0 32
  • 前言 您將在本文當(dāng)中了解到,往網(wǎng)頁中添加數(shù)據(jù),從傳統(tǒng)的dom操作過渡到數(shù)據(jù)層操作,實現(xiàn)同一個目標(biāo),兩種不同的方式....
    itclanCoder閱讀 26,244評論 1 12
  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內(nèi)容,還有我對于 Vue 1.0 印象不深的內(nèi)容。關(guān)于...
    云之外閱讀 5,177評論 0 29
  • # 傳智播客vue 學(xué)習(xí)## 1. 什么是 Vue.js* Vue 開發(fā)手機 APP 需要借助于 Weex* Vu...
    再見天才閱讀 3,789評論 0 6
  • 有些人問我為什么放著簡筆的愛不寫,為什么總喜歡用愛這個繁體字,因為愛需要用心才能真正成為愛啊,在我的潛意識里,我可...
    愛昔閱讀 183評論 0 1

友情鏈接更多精彩內(nèi)容