亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Flutter Element概念簡明分析

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

一 Element 概念

這個玩意的概念。到底是什么 ?

官方解釋是在樹中特定位置的實例。

二 繼承關(guān)系

element 有 ComponentElement 和 RenderObjectElement 之分

ComponentElement

class StatelessElement extends ComponentElement

class StatefulElement extends ComponentElement

三 生命周期

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

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

mount 方法負責注入任何child widgets,并且會在有需要·的時候,會調(diào)用attachRenderObject

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

四 方法分析

Element 這個抽象類中有一個方法 叫做 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插入到渲染樹中,插入到渲染樹后的element就處于“active”狀態(tài),處于“active”狀態(tà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概念簡明分析的文章就介紹到這了,更多相關(guān)Flutter Element內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論