溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Flutter渲染原理是什么

發(fā)布時(shí)間:2023-04-18 10:21:57 來源:億速云 閱讀:112 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Flutter渲染原理是什么的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Flutter渲染原理是什么文章都會(huì)有所收獲,下面我們一起來看看吧。

Widget Element RenderObject之間的關(guān)系

1 Widget

在Flutter 中,萬物皆是Widget,無論是可見的還是功能型的。一切都是Widget.

官方文檔中說的Widget 使用配置和狀態(tài)來描述View 界面應(yīng)該長什么樣子。

它不僅可以表示UI元素,也可以表示一些功能性的組件如:用于手勢(shì)檢測(cè)的 GestureDetector、用于APP主題數(shù)據(jù)傳遞的Theme、布局元素等等

兩個(gè)重要的方法

一個(gè)是通過 createElement 來創(chuàng)建 Element 對(duì)象的,

一個(gè)是根據(jù) key 來決定更新行為的 canUpdate 方法。

在這個(gè)方法中會(huì)對(duì)比runtimeType (也就是widget 的類型)和 key 是否相同

@immutable
abstract class Widget extends DiagnosticableTree {
  /// Initializes [key] for subclasses.
  const Widget({this.key});
  final Key? key;
  @protected
  @factory
  Element createElement();
  /// A short, textual description of this widget.
  @override
  String toStringShort() {
    final String type = objectRuntimeType(this, 'Widget');
    return key == null ? type : '$type-$key';
  }
  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.defaultDiagnosticsTreeStyle = DiagnosticsTreeStyle.dense;
  }
  @override
  @nonVirtual
  bool operator ==(Object other) => super == other;
  @override
  @nonVirtual
  int get hashCode => super.hashCode;
  static bool canUpdate(Widget oldWidget, Widget newWidget) {
    return oldWidget.runtimeType == newWidget.runtimeType &&
        oldWidget.key == newWidget.key;
  }
  // Return a numeric encoding of the specific `Widget` concrete subtype.
  // This is used in `Element.updateChild` to determine if a hot reload modified the
  // superclass of a mounted element's configuration. The encoding of each `Widget`
  // must match the corresponding `Element` encoding in `Element._debugConcreteSubtype`.
  static int _debugConcreteSubtype(Widget widget) {
    return widget is StatefulWidget
        ? 1
        : widget is StatelessWidget
            ? 2
            : 0;
  }
}

2 Element

Element 就是一個(gè)Widget 的實(shí)例,在樹中詳細(xì)的位置。

An instantiation of a Widget at a particular location in the tree

3 RenderObject

渲染樹上的一個(gè)對(duì)象。負(fù)責(zé)具體布局和繪制這些事情。

4 結(jié)合圖說一下其三者的關(guān)系

Flutter渲染原理是什么

從創(chuàng)建到渲染的流程 :

根據(jù)Widget 生成Element,然后創(chuàng)建響應(yīng)的RenderObject并且關(guān)聯(lián)到Element.renderObject 屬性。最后再通過RenderObject 來完成布局和繪制。

依賴關(guān)系:

Element 樹根據(jù)Widget 樹生成,而渲染樹又依賴于widget 樹。

5 一些小問題

widget 和 element 是一一對(duì)應(yīng)的嗎 ? 為什么 ?

答:是一一對(duì)應(yīng)的。

因?yàn)?abstract class Widget ,本身是一個(gè)抽象類,這個(gè)抽象類中有一個(gè)抽象方法叫做createElement(),子類必須實(shí)現(xiàn)這個(gè)抽象方法,所以是一一對(duì)應(yīng)的。

widget 和 renderObject 是一一對(duì)應(yīng)的嗎 ? 為什么 ?

答:不是的

因?yàn)橹挥羞@個(gè)widget 繼承自RenderObjectWidget 的時(shí)候,才會(huì)有對(duì)應(yīng)的renderObject

像類似 Padding , Row,SizedBox,Center 這種組件繼承自RenderObjectWidget的組件會(huì)有一一對(duì)應(yīng)的關(guān)系

//class Padding extends SingleChildRenderObjectWidget
// Padding();
// class Flex extends MultiChildRenderObjectWidget
// Row()

BuildContext 是什么 ?

答:是Element,不管是StatefulWidget 還是StatelessWidget 都會(huì)重寫父類的build 方法,

build 方法傳入的一個(gè)參數(shù)叫做BuildContext, 我們拿StatelessWidget來說,其本身創(chuàng)建一個(gè)StatelessElement,而在這個(gè)Element內(nèi)部重寫StatelessElement父類的build方法,而在這個(gè)build方法內(nèi)部會(huì)調(diào)用_widget.build 方法,并且把this傳遞進(jìn)去。那么這個(gè)this 就是element 。

/// An [Element] that uses a [StatelessWidget] as its configuration.
class StatelessElement extends ComponentElement {
  /// Creates an element that uses the given widget as its configuration.
  StatelessElement(StatelessWidget super.widget);
  @override
  Widget build() => (widget as StatelessWidget).build(this);
  @override
  void update(StatelessWidget newWidget) {
    super.update(newWidget);
    assert(widget == newWidget);
    _dirty = true;
    rebuild();
  }
}

Widget 頻繁更改創(chuàng)建是否會(huì)影響性能?復(fù)用和更新機(jī)制是什么樣的?

不會(huì)影響性能,因?yàn)橹皇且恍┡渲眯畔?,沒有有布局渲染到頁面上去。中間層Element 會(huì)通過widget 的runtimeType 和 Key 來對(duì)比是否進(jìn)行更新操作。

Build 方法會(huì)在什么時(shí)候調(diào)用 ?

Element 創(chuàng)建完畢之后會(huì)調(diào)用mount 方法,對(duì)于非渲染的ComponentElement 來說,mount主要執(zhí)行的是Widget 中的build 方法。在StatelessElement 中直接使用的是 widget.build(this),

而在StatefullWidget 方法中,通過的是state.build(this)。在StatefulElement 這個(gè)類中,

初始化列表的給state 進(jìn)行了賦值操作。通過widget調(diào)用createState方法之后,把state賦值給自己的_state 屬性。

StatefulElement(StatefulWidget widget)

: _state = widget.createState(),

createState 方法什么時(shí)候調(diào)用?

答:創(chuàng)建Element 的時(shí)候。

Flutter 會(huì)在遍歷 Widget 樹時(shí)調(diào)用 Widget 里面的 createElement 方法去生成對(duì)應(yīng)節(jié)點(diǎn)的 Element 對(duì)象,同時(shí)執(zhí)行 StatefulWidget 里面的 createState 方法創(chuàng)建 state,并且賦值給 Element 里的 _state 屬性,當(dāng)前 widget 也同時(shí)賦值給了 state 里的_widget,state 里面有個(gè) widget 的get 方法可以獲取到 _widget 對(duì)象。

關(guān)于“Flutter渲染原理是什么”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Flutter渲染原理是什么”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI