欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter Element概念簡(jiǎn)明分析

 更新時(shí)間:2023年04月13日 09:30:54   作者:iOS_Apple  
Flutter 中 Element 作用的是作為中樞來(lái)管理和調(diào)度Widget和RenderObject,這里我們主要說(shuō)一下RenderObjectWidget 來(lái)主要說(shuō)一下Element 的生命周期,這里我刪除了一些assert 的方法,方便查看

一 Element 概念

這個(gè)玩意的概念。到底是什么 ?

官方解釋是在樹(shù)中特定位置的實(shí)例。

二 繼承關(guān)系

element 有 ComponentElement 和 RenderObjectElement 之分

ComponentElement

class StatelessElement extends ComponentElement

class StatefulElement extends ComponentElement

三 生命周期

1 framework 通過(guò)在將要被用來(lái)作為Element的初始配置的widget 上調(diào)用其createElement 方法來(lái)創(chuàng)建一個(gè)element

2 framework 通過(guò)調(diào)用mount 方法 將一個(gè)新創(chuàng)建的element 加入樹(shù)中給定的父節(jié)點(diǎn)的插槽下面。

mount 方法負(fù)責(zé)注入任何child widgets,并且會(huì)在有需要·的時(shí)候,會(huì)調(diào)用attachRenderObject

將關(guān)聯(lián)的render objects 添加到渲染樹(shù)中 render tree 中。到這一步的時(shí)候,element 會(huì)進(jìn)入active 狀態(tài),并且會(huì)顯示在屏幕上方。

四 方法分析

Element 這個(gè)抽象類中有一個(gè)方法 叫做 mount 方法 。

  /// Add this element to the tree in the given slot of the given parent.
  ///
  /// The framework calls this function when a newly created element is added to
  /// the tree for the first time. Use this method to initialize state that
  /// depends on having a parent. State that is independent of the parent can
  /// more easily be initialized in the constructor.
  ///
  /// This method transitions the element from the "initial" lifecycle state to
  /// the "active" lifecycle state.
  ///
  /// Subclasses that override this method are likely to want to also override
  /// [update], [visitChildren], [RenderObjectElement.insertRenderObjectChild],
  /// [RenderObjectElement.moveRenderObjectChild], and
  /// [RenderObjectElement.removeRenderObjectChild].
  ///
  /// Implementations of this method should start with a call to the inherited
  /// method, as in `super.mount(parent, newSlot)`.
  @mustCallSuper
  void mount(Element? parent, Object? newSlot) {
    assert(_lifecycleState == _ElementLifecycle.initial);
    assert(widget != null);
    assert(_parent == null);
    assert(
        parent == null || parent._lifecycleState == _ElementLifecycle.active);
    assert(slot == null);
    _parent = parent;
    _slot = newSlot;
    _lifecycleState = _ElementLifecycle.active;
    _depth = _parent != null ? _parent!.depth + 1 : 1;
    if (parent != null) {
      // Only assign ownership if the parent is non-null. If parent is null
      // (the root node), the owner should have already been assigned.
      // See RootRenderObjectElement.assignOwner().
      _owner = parent.owner;
    }
    assert(owner != null);
    final Key? key = widget.key;
    if (key is GlobalKey) {
      owner!._registerGlobalKey(key, this);
    }
    _updateInheritance();
    attachNotificationTree();
  }

renderObjectElement 的mount 方法

其主要作用 將element相關(guān)聯(lián)的renderObject插入到渲染樹(shù)中,插入到渲染樹(shù)后的element就處于“active”狀態(tài),處于“active”狀態(tài)后就可以顯示在屏幕上了。

此處可以看出來(lái),RenderObject? _renderObject; element 是持有renderObject 的引用的

  @override
  void mount(Element? parent, Object? newSlot) {
    super.mount(parent, newSlot);
    assert(() {
      _debugDoingBuild = true;
      return true;
    }());
    _renderObject = (widget as RenderObjectWidget).createRenderObject(this);
    assert(!_renderObject!.debugDisposed!);
    assert(() {
      _debugDoingBuild = false;
      return true;
    }());
    assert(() {
      _debugUpdateRenderObjectOwner();
      return true;
    }());
    assert(_slot == newSlot);
    attachRenderObject(newSlot);
    _dirty = false;
  }

到此這篇關(guān)于Flutter Element概念簡(jiǎn)明分析的文章就介紹到這了,更多相關(guān)Flutter Element內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論