)
一、前言在Flutter的三棵樹中 Widget 只是不可變的 UI 配置模板真正承載視圖實例、生命周期、狀態(tài)管理、刷新調(diào)度、節(jié)點復(fù)用的核心類是 Element。如果不理解 Element就永遠(yuǎn)看不懂 Key復(fù)用原理、State 生命周期、InheritedWidget 局部刷新、setState 渲染機制。本文從概念、三棵樹關(guān)系、源碼、復(fù)用機制、生命周期、臟節(jié)點渲染、實戰(zhàn) Demo、面試誤區(qū)全方位吃透 Element。二、三層架構(gòu)核心認(rèn)知Widget / Element / RenderObject1. 三層樹定位與職責(zé)Flutter UI 采用經(jīng)典三層架構(gòu)分層明確、職責(zé)單一Widget配置層immutable靜態(tài)配置只描述UI結(jié)構(gòu)、樣式、屬性無狀態(tài)、無生命周期每次重建都會 new 新對象。Element實例核心層Widget 的運行時實例掛載在視圖樹上。負(fù)責(zé)生命周期、狀態(tài)持有、節(jié)點復(fù)用、依賴收集、臟節(jié)點刷新調(diào)度。RenderObject渲染層最終渲染對象負(fù)責(zé)布局、測量、繪制、合成對接屏幕像素。2. 通俗類比經(jīng)典易懂Widget 圖紙可無限復(fù)用、隨時改版不能直接建成建筑。Element 建筑物實例真實存在、可翻新復(fù)用、擁有完整生命周期。RenderObject 裝修渲染真正上色、布局、展示給用戶的效果。三、三棵樹映射關(guān)系1.三棵樹之間的映射關(guān)系三棵樹的綁定關(guān)系是 Flutter 最高頻面試考點一對一/一對多關(guān)系固定不可改變Widget → Element一對多 同一份配置模板可以在頁面多處生成多個獨立視圖實例。Element → Widget一對一 同一時刻一個視圖實例只綁定一份 Widget 配置。Element → RenderObject一對一 渲染型 Element 唯一對應(yīng)一個渲染對象保證布局繪制穩(wěn)定。2.驗證三棵樹之間的依賴關(guān)系1.Widget和Element之間的依賴關(guān)系我們設(shè)計一個Demo驗證下Widget和Element之間的映射關(guān)系。圖1.Widget和Element映射關(guān)系demo共享的代碼如下在下面的代碼中build函數(shù)中的context就是_SharedBox對應(yīng)的Element。調(diào)用Widget的build函數(shù)的時候我們打印下持有的Widget的哈希值以及Widget持有的Element的哈希值。class _SharedBox extends StatelessWidget { const _SharedBox({required this.label, required this.slotLabel}); final String label; final String slotLabel; override Widget build(BuildContext context) { // context 本身就是本組件對應(yīng)的 ElementBuildContext 的實現(xiàn)就是 Element final element context as Element; debugPrint( 【_SharedBox】Element 實例$element 持有的 widget${element.widget.hashCode}, ); final scheme Theme.of(context).colorScheme; return Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: scheme.surfaceContainerHighest.withValues(alpha: 0.6), borderRadius: BorderRadius.circular(10), border: Border.all(color: scheme.outlineVariant), ), child: Column( children: [ Container( padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 2), decoration: BoxDecoration( color: scheme.primary, borderRadius: BorderRadius.circular(10), ), child: Text( slotLabel, style: TextStyle( color: scheme.onPrimary, fontSize: 12, fontWeight: FontWeight.bold, ), ), ), const SizedBox(height: 8), Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: scheme.primaryContainer.withValues(alpha: 0.5), borderRadius: BorderRadius.circular(8), border: Border.all(color: scheme.primary.withValues(alpha: 0.5)), ), child: Text( label, textAlign: TextAlign.center, style: const TextStyle(fontWeight: FontWeight.w600, fontSize: 13), ), ), ], ), ); } }控制臺輸出日志如下圖2.控制臺輸出日志從控制臺的打印日志我們可以看出當(dāng)前頁面的Widget實例化了三個Element示例但是每一個element持有的Widget是相同的。因此我們得到的結(jié)論是Widget和Element是一對多的關(guān)系Element和Widget是一對一的關(guān)系。2.Element和RenderObject之間的映射關(guān)系我們使用下面的UI驗證下Element和RenderObject之間的映射關(guān)系。圖3.驗證Element和RenderObject之間的依賴關(guān)系代碼如下import package:flutter/material.dart; /// 演示 Element → RenderObject 的一一映射 /// 每個 RenderObjectElement如 ColoredBox、Text、Padding掛載時都會創(chuàng)建 /// 并持有自己的 RenderObject。兩者一一對應(yīng)、同生共死—— /// 父級重建時 Element 復(fù)用RenderObject 也隨之復(fù)用只更新其屬性如顏色 /// 而不是重新創(chuàng)建。 class ElementRenderObjectMappingPage extends StatefulWidget { const ElementRenderObjectMappingPage({super.key}); override StateElementRenderObjectMappingPage createState() _ElementRenderObjectMappingPageState(); } /// 一次探測結(jié)果某個位置上 Element 與 RenderObject 的實例信息 class _ProbeInfo { const _ProbeInfo({ this.elementHash, this.elementType, this.renderHash, this.renderType, }); final int? elementHash; final String? elementType; final int? renderHash; final String? renderType; } class _ElementRenderObjectMappingPageState extends StateElementRenderObjectMappingPage { final GlobalKey _keyA GlobalKey(); final GlobalKey _keyB GlobalKey(); final GlobalKey _keyC GlobalKey(); _ProbeInfo _infoA const _ProbeInfo(); _ProbeInfo _infoB const _ProbeInfo(); _ProbeInfo _infoC const _ProbeInfo(); int _count 0; /// 每次重建輪換一次顏色證明 RenderObject 復(fù)用但屬性被更新 static const _palettes [ [Color(0xFFE53935), Color(0xFFFB8C00), Color(0xFF1E88E5)], [Color(0xFF1E88E5), Color(0xFFE53935), Color(0xFFFB8C00)], [Color(0xFFFB8C00), Color(0xFF1E88E5), Color(0xFFE53935)], ]; override void initState() { super.initState(); _refresh(); } /// 本幀構(gòu)建完成后探測各位置的 Element 與 RenderObject void _refresh() { WidgetsBinding.instance.addPostFrameCallback((_) { if (!mounted) return; setState(() { _infoA _probe(_keyA); _infoB _probe(_keyB); _infoC _probe(_keyC); }); }); } /// 探測 [key] 對應(yīng)位置拿到 Element 本身 它創(chuàng)建的 RenderObject _ProbeInfo _probe(GlobalKey key) { final ctx key.currentContext; if (ctx null) return const _ProbeInfo(); // ColoredBox 是 SingleChildRenderObjectWidget // 其 Element 是 RenderObjectElementrenderObject 一定非空。 final element ctx as RenderObjectElement; final render element.renderObject; return _ProbeInfo( elementHash: element.hashCode, elementType: element.runtimeType.toString(), renderHash: render.hashCode, renderType: render.runtimeType.toString(), ); } override Widget build(BuildContext context) { final theme Theme.of(context); final colors _palettes[_count % _palettes.length]; return Scaffold( appBar: AppBar(title: const Text(Element ? RenderObject 一一對應(yīng))), body: SingleChildScrollView( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Card( color: theme.colorScheme.primaryContainer, child: const Padding( padding: EdgeInsets.all(16), child: Text( 三棵樹中Element 是「協(xié)調(diào)者」每個 RenderObjectElement如 ColoredBox、Text、Padding 等掛載時都會創(chuàng)建并持有自己的 RenderObject。兩者一一對應(yīng)、同生共死——Element 復(fù)用則 RenderObject 復(fù)用重建時只更新 RenderObject 的屬性 而不是重新創(chuàng)建。, style: TextStyle(height: 1.6), ), ), ), const SizedBox(height: 20), _SectionTitle(title: 實驗 · Element 與 RenderObject 一一對應(yīng)), const SizedBox(height: 4), const _HintText( text: 三個位置各掛載一個 ColoredBox探測每個位置的 Element 與它創(chuàng)建的 RenderObject點「父級重建」觀察復(fù)用關(guān)系與顏色屬性更新。, ), const SizedBox(height: 12), Row( children: [ Expanded(child: _buildSlot(_keyA, colors[0], A 槽)), const SizedBox(width: 8), Expanded(child: _buildSlot(_keyB, colors[1], B 槽)), const SizedBox(width: 8), Expanded(child: _buildSlot(_keyC, colors[2], C 槽)), ], ), const SizedBox(height: 12), _InfoRow(slot: A, info: _infoA), _InfoRow(slot: B, info: _infoB), _InfoRow(slot: C, info: _infoC), const SizedBox(height: 20), Center( child: FilledButton.icon( onPressed: () { setState(() _count); _refresh(); }, icon: const Icon(Icons.refresh), label: Text(父級重建第 $_count 次), ), ), const SizedBox(height: 8), const _HintText( text: 重建后Element 與 RenderObject 的 #hash 都不變復(fù)用同一對象 只有 Widget 換成新實例、RenderObject 的屬性顏色被更新。, textAlign: TextAlign.center, ), ], ), ), ); } /// 一個帶 key 的彩色盒子key 直接掛在 ColoredBoxSingleChildRenderObjectWidget /// 上這樣探測到的 Element 是 RenderObjectElement能直接拿到 renderObject。 Widget _buildSlot(GlobalKey key, Color color, String label) { return Column( mainAxisSize: MainAxisSize.min, children: [ ColoredBox( key: key, color: color, child: const SizedBox(width: double.infinity, height: 64), ), const SizedBox(height: 4), Text( label, style: const TextStyle(fontSize: 12, fontWeight: FontWeight.bold), ), ], ); } } class _SectionTitle extends StatelessWidget { const _SectionTitle({required this.title}); final String title; override Widget build(BuildContext context) { return Text( title, style: Theme.of(context) .textTheme .titleSmall ?.copyWith(fontWeight: FontWeight.bold), ); } } class _HintText extends StatelessWidget { const _HintText({required this.text, this.textAlign}); final String text; final TextAlign? textAlign; override Widget build(BuildContext context) { return Text( text, textAlign: textAlign, style: Theme.of(context) .textTheme .bodySmall ?.copyWith(color: Colors.black54, height: 1.5), ); } } /// 每行展示槽位名 該位置 Element 的類型/hash RenderObject 的類型/hash class _InfoRow extends StatelessWidget { const _InfoRow({required this.slot, required this.info}); final String slot; final _ProbeInfo info; override Widget build(BuildContext context) { final scheme Theme.of(context).colorScheme; return Padding( padding: const EdgeInsets.symmetric(vertical: 3), child: Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: scheme.surfaceContainerHighest.withValues(alpha: 0.5), borderRadius: BorderRadius.circular(8), ), child: Row( children: [ SizedBox( width: 40, child: Text( $slot 位, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 13), ), ), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( Element : ${info.elementType ?? —} #${_hex(info.elementHash)}, key: ValueKey(element_$slot), style: const TextStyle( fontFamily: monospace, fontSize: 11, color: Colors.black87, ), overflow: TextOverflow.ellipsis, ), const SizedBox(height: 2), Text( Render : ${info.renderType ?? —} #${_hex(info.renderHash)}, key: ValueKey(render_$slot), style: const TextStyle( fontFamily: monospace, fontSize: 11, color: Colors.indigo, ), overflow: TextOverflow.ellipsis, ), ], ), ), ], ), ), ); } String _hex(int? hash) hash?.toRadixString(16) ?? —; }點擊按鈕之后控制臺打印日志如下因此我們可以看出Element和RenderObject之間是一對一的關(guān)系。四、Element 繼承體系我們從源碼的角度分析下Element。4.1. Element頂層抽象類定義Element是抽象類實現(xiàn)了BuildContext這也是為什么context本質(zhì)就是當(dāng)前 Element 實例。// Flutter 官方源碼精簡版 abstract class Element extends DiagnosticableTree implements BuildContext { Element(this._widget); // 當(dāng)前Element綁定的Widget配置 Widget _widget; // 父Element Element? _parent; // 是否為臟節(jié)點需要重建 bool _dirty false; // 對外暴露當(dāng)前綁定的Widget override Widget get widget _widget; // 核心是否掛載在視圖樹上 bool get mounted _parent ! null; // 渲染對象僅RenderObjectElement有效 RenderObject? get renderObject; }核心解讀context 當(dāng)前 Element這是所有上下文操作的根源Element 天生持有_widget引用實現(xiàn) Element→Widget 一對一_dirty臟節(jié)點標(biāo)記是setState刷新的底層開關(guān)4.2 Element 兩大核心分支Flutter所有Element 只有兩類徹底對應(yīng)所有 WidgetComponentElement組合型 Element 對應(yīng)StatelessWidget/StatefulWidget不直接渲染只負(fù)責(zé)嵌套子 Widget、構(gòu)建子樹RenderObjectElement渲染型 Element 對應(yīng)ColoredBox/Text/Padding等持有RenderObject直接參與布局與繪制4.3 mount 掛載節(jié)點初始化mount是 Element 首次掛載到視圖樹的入口只執(zhí)行一次對應(yīng)組件初始化生命周期。mustCallSuper void mount(Element? parent, Object? newSlot) { _parent parent; _dirty true; // 首次掛載觸發(fā)首次構(gòu)建 _firstBuild(); } void _firstBuild() { rebuild(); }源碼鏈路創(chuàng)建Element →mount掛載 →_firstBuild→rebuild→ 執(zhí)行 build 生成 UI4.4 rebuild / performRebuild重建邏輯所有刷新初始化、setState、依賴更新最終都會走到performRebuild。void rebuild() { if (_dirty) { performRebuild(); } } // ComponentElement 重建實現(xiàn) override void performRebuild() { // 調(diào)用Widget.build()生成新子Widget final Widget newChild widget.build(this); // 核心對比新舊子節(jié)點走復(fù)用/重建邏輯 _child updateChild(_child, newChild, slot); _dirty false; }關(guān)鍵真相build()方法由 Element 主動調(diào)用并非系統(tǒng)自動調(diào)用重建核心不是無腦新建而是updateChild做差異化比對4.5 updateChild 差異化復(fù)用這是 Flutter高性能的最核心源碼決定節(jié)點是復(fù)用還是銷毀重建。Element? updateChild(Element? child, Widget? newWidget, Object? newSlot) { // 場景1新Widget為空直接移除舊節(jié)點 if (newWidget null) { if (child ! null) deactivateChild(child); return null; } // 場景2舊節(jié)點存在嘗試復(fù)用 if (child ! null) { // 核心判定類型Key一致 → 復(fù)用 if (child.widget.canUpdate(newWidget)) { child.update(newWidget); return child; } // 不滿足條件 → 銷毀舊節(jié)點 deactivateChild(child); } // 場景3無法復(fù)用 → 創(chuàng)建新Element并掛載 final Element newElement newWidget.createElement(); newElement.mount(this, newSlot); return newElement; }4.6 canUpdatecanUpdate就是我們前面說的「類型Key」雙條件判定源頭bool canUpdate(Widget oldWidget, Widget newWidget) { return oldWidget.runtimeType newWidget.runtimeType oldWidget.key newWidget.key; }全網(wǎng)所有Key 原理、State 復(fù)用、節(jié)點復(fù)用全部源自這行源碼無任何例外。4.7 StatefulElement 專屬源碼State 保存原理為什么 State 不會跟著 Widget 重建而丟失看構(gòu)造源碼一目了然class StatefulElement extends ComponentElement { StatefulElement(StatefulWidget widget) : _state widget.createState(), super(widget) { // 雙向綁定State持有Element、Element持有State _state._element this; _state._widget widget; // 初始化生命周期 _state.initState(); } final State _state; }深度解讀State在 Element 創(chuàng)建時只初始化一次Element 復(fù)用時不會重新走構(gòu)造所以State永久保留真正存狀態(tài)的是 StatefulElement不是 Widget4.8 RenderObjectElement 一對一綁定源碼解釋為什么 Element 和 RenderObject 永遠(yuǎn)一對一、同生共死abstract class RenderObjectElement extends Element { RenderObject? _renderObject; override void mount(Element? parent, Object? newSlot) { super.mount(parent, newSlot); // 掛載時唯一創(chuàng)建一次RenderObject _renderObject widget.createRenderObject(this); // 綁定到渲染樹 attachRenderObject(newSlot); } // 更新僅刷新屬性不重建對象 override void update(Widget newWidget) { super.update(newWidget); widget.updateRenderObject(this, _renderObject!); } }一對一本質(zhì)mount階段 唯一創(chuàng)建一次 RenderObject后續(xù)更新只調(diào)用updateRenderObject修改屬性Element 不銷毀RenderObject 永遠(yuǎn)不重建所有生命周期、刷新、復(fù)用邏輯全部由這三個子類實現(xiàn)。五、Element復(fù)用機制1. 復(fù)用唯一條件滿足以下兩點State/Element 復(fù)用不銷毀重建runtimeType 完全一致Key 完全相等2. 底層判定源碼bool canUpdate(Widget oldWidget, Widget newWidget) { return oldWidget.runtimeType newWidget.runtimeType oldWidget.key newWidget.key; }源碼解讀復(fù)用成功不走 initState只走 didUpdateWidget build復(fù)用失敗銷毀 State、Element重建全新實例3. Key 解決列表狀態(tài)錯位原理無Key時同級同類型組件默認(rèn)復(fù)用 Element導(dǎo)致狀態(tài)跟隨位置、不跟隨數(shù)據(jù)。 加入 ValueKey 后數(shù)據(jù)改變 Key 改變強制不復(fù)用狀態(tài)跟隨數(shù)據(jù)。4. 實戰(zhàn) DemoKey 控制復(fù)用下面用一個可運行的 Demo 直觀展示 Key 對 Element 復(fù)用的控制效果。核心思路同一份列表數(shù)據(jù)一組子組件不加 Key另一組加 ValueKey點擊「交換位置」后觀察兩組計數(shù)器的狀態(tài)差異。import package:flutter/material.dart; class ElementReuseKeyDemo extends StatefulWidget { const ElementReuseKeyDemo({super.key}); override StateElementReuseKeyDemo createState() _ElementReuseKeyDemoState(); } class _ElementReuseKeyDemoState extends StateElementReuseKeyDemo { final ListString list [紅色, 藍(lán)色]; void swap() { setState(() { list.insert(1, list.removeAt(0)); }); } override Widget build(BuildContext context) { return Scaffold( body: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text(無 Key復(fù)用 Element 狀態(tài)錯位), Row( children: list.map((e) _CounterItem(colorName: e)).toList(), ), const SizedBox(height: 30), const Text(有 ValueKey不復(fù)用 狀態(tài)正常), Row( children: list .map((e) _CounterItem( key: ValueKey(e), colorName: e, )) .toList(), ), const SizedBox(height: 30), ElevatedButton(onPressed: swap, child: const Text(交換位置)) ], ), ); } } class _CounterItem extends StatefulWidget { final String colorName; const _CounterItem({super.key, required this.colorName}); override State_CounterItem createState() _CounterItemState(); } class _CounterItemState extends State_CounterItem { int count 0; override Widget build(BuildContext context) { return GestureDetector( onTap: () setState(() count), child: Container( width: 120, height: 120, margin: const EdgeInsets.all(10), color: widget.colorName 紅色 ? Colors.red : Colors.blue, child: Center( child: Text( $count, style: const TextStyle(color: Colors.white, fontSize: 24), ), ), ), ); } }運行效果解讀無 Key 組交換位置后兩個計數(shù)器 Element 被原樣復(fù)用State 跟隨位置而非數(shù)據(jù)導(dǎo)致「紅色」和「藍(lán)色」的計數(shù)錯位。有 ValueKey 組數(shù)據(jù)變化時 Key 隨之變化canUpdate 判定失敗強制重建State 跟隨數(shù)據(jù)計數(shù)始終正確。六、Element 臟節(jié)點渲染機制setState 底層原理1. setState 源碼void setState(VoidCallback fn) { fn(); _element專業(yè)解釋setState 并不會立即刷新頁面只是將當(dāng)前 Element 標(biāo)記為臟節(jié)點dirty由 Flutter 渲染引擎在下一幀統(tǒng)一批量重建實現(xiàn)高性能批量更新。七、Element 驅(qū)動的生命周期源碼解析所有 State 生命周期都是 Element 主動調(diào)用State 只是被動回調(diào)。理解這一節(jié)就能徹底搞懂 initState、didUpdateWidget、didChangeDependencies 這些回調(diào)到底是誰在什么時機觸發(fā)的。1. 初始化流程源碼initStateState 的創(chuàng)建與 initState 回調(diào)都發(fā)生在 StatefulElement 的構(gòu)造函數(shù)中。也就是說只要 Element 被創(chuàng)建State 就隨之誕生并完成初始化class StatefulElement extends ComponentElement { StatefulElement(StatefulWidget widget) : _state widget.createState(), super(widget) { // 雙向綁定State 持有 Element、Element 持有 State _state._element this; _state._widget widget; // 初始化生命周期僅首次創(chuàng)建時執(zhí)行一次 _state.initState(); } final State _state; }createState()由 Widget 提供創(chuàng)建出對應(yīng)的 State 實例構(gòu)造函數(shù)內(nèi)完成_element與_widget的雙向綁定State 從此能訪問 context 和 widgetinitState()在構(gòu)造階段被主動調(diào)用且只執(zhí)行一次適合做數(shù)據(jù)初始化、添加監(jiān)聽2. 復(fù)用更新流程didUpdateWidget當(dāng)父組件重建、且新舊 Widget 滿足canUpdate類型 Key 一致時Element 會被復(fù)用而不是重建。此時 Flutter 調(diào)用update進(jìn)而觸發(fā)didUpdateWidget回調(diào)override void update(Widget newWidget) { super.update(newWidget); // 通知 StateWidget 配置已更新可對比 oldWidget 做業(yè)務(wù)處理 _state.didUpdateWidget(oldWidget); }源碼解讀復(fù)用更新時不會重新走構(gòu)造函數(shù)因此initState不會再次執(zhí)行didUpdateWidget接收舊 Widget方便對比新舊參數(shù)差異更新業(yè)務(wù)數(shù)據(jù)更新完成后會繼續(xù)走build用新配置重建 UI3. 依賴更新流程didChangeDependencies當(dāng)組件依賴的InheritedWidget數(shù)據(jù)發(fā)生變化時Element 會通知所有依賴它的子 Element觸發(fā)didChangeDependencies并標(biāo)記重建void notifyClients(InheritedWidget oldWidget) { for (final Element dependent in _dependents) { // 通知依賴者全局依賴已變化 dependent.didChangeDependencies(); // 標(biāo)記為臟節(jié)點下一幀重建 dependent.markNeedsBuild(); } }源碼解讀_dependents是訂閱了該 InheritedWidget 的 Element 集合依賴更新時先回調(diào)didChangeDependencies再標(biāo)記臟節(jié)點等待重建典型場景主題切換、語言切換、全局配置變更4. 生命周期核心區(qū)別面試絕殺表生命周期觸發(fā)時機核心場景initStateElement 首次創(chuàng)建初始化數(shù)據(jù)、監(jiān)聽didUpdateWidgetElement 復(fù)用、父組件傳參更新對比 oldWidget 更新業(yè)務(wù)didChangeDependenciesInheritedWidget 依賴變化主題、語言、全局配置變更disposeElement 永久銷毀釋放資源、取消訂閱5. 完整生命周期執(zhí)行鏈路首次掛載Widget創(chuàng)建 → Element創(chuàng)建 → initState → didChangeDependencies → build復(fù)用更新父重建 → 復(fù)用Element → didUpdateWidget → build依賴更新Inherited更新 → didChangeDependencies → build銷毀deactivate → dispose生命周期日志 Demo下面用一個可運行的 Demo 直觀展示各生命周期回調(diào)的執(zhí)行順序建議復(fù)制到項目中運行觀察控制臺輸出class LifeCycleMonitorWidget extends StatefulWidget { final String content; const LifeCycleMonitorWidget({super.key, required this.content}); override StateLifeCycleMonitorWidget createState() { debugPrint(創(chuàng)建新 Element State); return _LifeCycleMonitorWidgetState(); } } class _LifeCycleMonitorWidgetState extends StateLifeCycleMonitorWidget { override void initState() { super.initState(); debugPrint(initState 初始化); } override void didUpdateWidget(covariant LifeCycleMonitorWidget oldWidget) { super.didUpdateWidget(oldWidget); debugPrint(didUpdateWidget 觸發(fā)參數(shù)更新復(fù)用Element); } override void didChangeDependencies() { super.didChangeDependencies(); debugPrint(didChangeDependencies 觸發(fā)全局依賴更新); } override Widget build(BuildContext context) { debugPrint(build 執(zhí)行); return Text(widget.content); } override void dispose() { debugPrint(dispose 銷毀 Element); super.dispose(); } }受制于博客字?jǐn)?shù)限制和實際閱讀效果我們將在下一章節(jié)驗證下這篇博客的一些結(jié)論。