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è)抽象類(lèi)中有一個(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)文章
Kotlin文件讀寫(xiě)與SharedPreferences存儲(chǔ)功能實(shí)現(xiàn)方法
SharedPreferences是安卓平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類(lèi),用來(lái)保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時(shí),將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時(shí),再?gòu)腟haredPreferences中將值取出2022-12-12
詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案
這篇文章主要介紹了詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12
淺談Android應(yīng)用內(nèi)懸浮控件實(shí)踐方案總結(jié)
本篇文章主要介紹了淺談Android應(yīng)用內(nèi)懸浮控件實(shí)踐方案總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
點(diǎn)擊微信內(nèi)網(wǎng)頁(yè)a標(biāo)簽直接跳轉(zhuǎn)打開(kāi)淘寶APP的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于如何實(shí)現(xiàn)點(diǎn)擊微信內(nèi)網(wǎng)頁(yè)a標(biāo)簽直接跳轉(zhuǎn)打開(kāi)淘寶APP的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-11-11
Android中的人臉檢測(cè)的示例代碼(靜態(tài)和動(dòng)態(tài))
本篇文章主要介紹了Android中的人臉檢測(cè)的示例代碼(靜態(tài)和動(dòng)態(tài)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android實(shí)現(xiàn)左側(cè)滑動(dòng)菜單
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)左側(cè)滑動(dòng)菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Android自定義view實(shí)現(xiàn)仿抖音點(diǎn)贊效果
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)仿抖音點(diǎn)贊效果,代碼簡(jiǎn)單易懂非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05
Eclipse+ADT+Android SDK搭建安卓開(kāi)發(fā)環(huán)境的實(shí)現(xiàn)步驟
這篇文章主要介紹了Eclipse+ADT+Android SDK搭建安卓開(kāi)發(fā)環(huán)境的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Android開(kāi)發(fā)常用經(jīng)典代碼段集錦
這篇文章主要介紹了Android開(kāi)發(fā)常用經(jīng)典代碼段,涉及Android開(kāi)發(fā)過(guò)程中針對(duì)手機(jī)、聯(lián)系人、圖片、存儲(chǔ)卡等的相關(guān)操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-02-02

