Flutter Element概念簡(jiǎn)明分析
一 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)文章
Android媒體通知欄多系統(tǒng)適配實(shí)例講解
對(duì)于Android來(lái)說(shuō)其中一項(xiàng)很方便的操作便是下拉菜單,下拉菜單欄可以快捷打開(kāi)某項(xiàng)設(shè)置,這篇文章主要給大家介紹了關(guān)于Android通知欄增加快捷開(kāi)關(guān)的功能實(shí)現(xiàn),需要的朋友可以參考下2023-04-04
Android使用SwipeListView實(shí)現(xiàn)類似QQ的滑動(dòng)刪除效果
這篇文章主要介紹了Android使用SwipeListView實(shí)現(xiàn)類似QQ的滑動(dòng)刪除效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android仿微信QQ聊天頂起輸入法不頂起標(biāo)題欄的問(wèn)題
這篇文章主要介紹了Android之仿微信QQ聊天頂起輸入法不頂起標(biāo)題欄問(wèn)題,本文實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Android時(shí)光軸實(shí)現(xiàn)淘寶物流信息瀏覽效果
這篇文章主要為大家詳細(xì)介紹了Android時(shí)光軸實(shí)現(xiàn)淘寶物流信息瀏覽效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android 詳解Studio引用Library與導(dǎo)入jar
這篇文章主要介紹了Android Studio引用Library與導(dǎo)入jar的相關(guān)資料,需要的朋友可以參考下2017-01-01
Kotlin中Lambda表達(dá)式與高階函數(shù)使用分析講解
lambda 本質(zhì)上是可以傳遞給函數(shù)的一小段代碼,Kotlin 與 Java 中的 Lambda 有一定的區(qū)別,除了對(duì) lambda 的全面支持外,還有內(nèi)聯(lián)函數(shù)等簡(jiǎn)潔高效的特性。下面我們來(lái)仔細(xì)看一下2022-12-12
在android開(kāi)發(fā)中進(jìn)行數(shù)據(jù)存儲(chǔ)與訪問(wèn)的多種方式介紹
很多時(shí)候我們的軟件需要對(duì)處理后的數(shù)據(jù)進(jìn)行存儲(chǔ)或再次訪問(wèn),Android為數(shù)據(jù)存儲(chǔ)提供了多種方式,首先給大家介紹使用文件如何對(duì)數(shù)據(jù)進(jìn)行存儲(chǔ),感興趣的朋友可以了解下哈2013-06-06

