国产睡熟迷奷白丝护士系列精品,免费国产精品专区,亚洲精选中文字幕,性欲福利视频网网站

  • <pre id="344ob"></pre>
      1. <track id="344ob"></track>
      2. <button id="344ob"></button><small id="344ob"></small>
          資訊財經(jīng)娛樂科技汽車時尚企業(yè)游戲商訊消費購物微商

          用Flutter做APP學(xué)習(xí)心得:Flutter widget的生命周期

          2020-03-23 09:18:37 來源: 閱讀:-

          第一次看文章的朋友可以關(guān)注我和我的專欄,會不定期發(fā)布大廠面試題、Android架構(gòu)技術(shù)知識點及解析等內(nèi)容,還有Android學(xué)習(xí)PDF+源碼筆記+面試文檔+進階視頻分享。



          前言

          最近一直在用flutter做app,感覺就像用vue或react寫web頁面一樣,蠻好玩的。為了加深自己對Flutter的理解,記錄一下自己的學(xué)習(xí)心得。

          Widget、StatefulWidget、StatelessWidget

          看源碼就知道這三者的關(guān)系了,代碼如下:

          abstract class Widget extends DiagnosticableTree {  const Widget({ this.key });  final Key key;  @protected  Element createElement();  static bool canUpdate(Widget oldWidget, Widget newWidget) {    return oldWidget.runtimeType == newWidget.runtimeType        &amp;&amp; oldWidget.key == newWidget.key;  }  }abstract class StatelessWidget extends Widget {  const StatelessWidget({ Key key }) : super(key: key);  @override  StatelessElement createElement() =&gt; StatelessElement(this);  @protected  Widget build(BuildContext context);  }abstract class StatefulWidget extends Widget {  const StatefulWidget({ Key key }) : super(key: key);  @override  createElement() =&gt; StatefulElement(this);  @protected  State createState();}復(fù)制代碼

          StatelessWidget沒有狀態(tài),可以類比react中的傻瓜組件;

          StatefulWidget有狀態(tài),狀態(tài)存在State對象中;所以一般說的生命周期都是指的是State的生命周期;

          abstract class State extends Diagnosticable {  void initState() { }  void didChangeDependencies() { }  Widget build(BuildContext context);  void setState(VoidCallback fn) {}  void deactivate() { }  void dispose() { }  void reassemble() { }  void didUpdateWidget(covariant T oldWidget) { }}復(fù)制代碼

          現(xiàn)象

          為了驗證State的生命周期,我的操作是從Home-&gt;page1-&gt;page2-&gt;page1-&gt;Home

          從Home-&gt;page1,這是初始化的過程

          I/flutter ( 4980): [debug],[lifeCycle], initStateI/flutter ( 4980): [debug],[lifeCycle], didChangeDependenciesI/flutter ( 4980): [debug],[lifeCycle], build復(fù)制代碼

          從page1-&gt;page2,相當(dāng)于page1被暫時移出

          I/flutter ( 4980): [debug],[lifeCycle], deactivateI/flutter ( 4980): [debug],[lifeCycle], didChangeDependenciesI/flutter ( 4980): [debug],[lifeCycle], build復(fù)制代碼

          從page2-&gt;page1,相當(dāng)于page1又被移回來了

          I/flutter ( 4980): [debug],[lifeCycle], deactivateI/flutter ( 4980): [debug],[lifeCycle], didChangeDependenciesI/flutter ( 4980): [debug],[lifeCycle], build復(fù)制代碼

          從page1-&gt;Home,相當(dāng)于page1被刪除

          I/flutter ( 4980): [debug],[lifeCycle], deactivateI/flutter ( 4980): [debug],[lifeCycle], dispose復(fù)制代碼

          生命周期的總結(jié)

          直接看圖,應(yīng)該很清晰了,

          上述的生命周期,有兩個地方需要重點理解:

          1. state對象的依賴發(fā)生變化時,這是當(dāng)state中通過InheritedWidget使用了父級widget的共享數(shù)據(jù),當(dāng)數(shù)據(jù)變化的時候,就會調(diào)用子widget的didChangeDependencies()

          2. widget重新構(gòu)建時,會調(diào)用state的didUpdateWidget();首先要知道,每個StatefulWidget都有一個state對應(yīng); 當(dāng)widget重新構(gòu)建的時候,會先調(diào)用widget的canUpdate方法來判斷是否需要更新;如果 key與runtimeType都一樣的,會返回true,表示可以對widget進行更新,從而調(diào)用state的didUpdateWidget();如果 key或runtimeType不一樣的時候,那就沒必要更新,直接刪除舊的,創(chuàng)建個新的就可以了。

          疑問

          在嘗試的時候,有個現(xiàn)象,當(dāng)widget頁面在可見到不可見之間切換的時候,調(diào)用的方法都是一樣的,都是deativate-&gt;didChangeDependencies-&gt;build;這里有個疑問,既然都已經(jīng)不可見了為啥還要build?或者說都build了為啥還能不可見?

          這個疑問應(yīng)該需要進一步挖掘Flutter的widget、element、render樹的關(guān)系,等俺進一步理解了Flutter UI原理后再來分享哈

          最后

          現(xiàn)在,很多公司的項目已經(jīng)上flutter,還有些公司在上flutter的路上

          學(xué)習(xí)要先人一步,關(guān)注我,私信【安卓】領(lǐng)取flutter學(xué)習(xí)視頻



          推薦閱讀:拍立得mini7c

          查看心情排行你看到此篇文章的感受是:


          • 支持

          • 高興

          • 震驚

          • 憤怒

          • 無聊

          • 無奈

          • 謊言

          • 槍稿

          • 不解

          • 標(biāo)題黨
          要聞排行
          精彩推薦