網站首頁 編程語言 正文
Flutter中的生命周期類似于Vue、React中的生命周期一樣,有初始化、狀態更新、停用、銷毀等。
在React中,組件分為函數式組件和類式組件,它們的區別就是一個無狀態、一個有狀態。那么在Flutter中亦是如此,它有兩種類,一種是無狀態類,一種是有狀態類。其生命周期的使用就是有狀態類的特定用法。
無狀態類
無狀態類內部有build方法,在表面上看 每次數據更新都會執行build方法。但實際上,在組件樹中,當每次數據發生變更時,無狀態類都會重新執行組件LessComponent對象。
class LessComponent extends StatelessWidget { const LessComponent({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Container(); } }
有狀態類
在有狀態類中,每次的數據發生變動,在組件樹中只會調用state下的build方法進行重新渲染。這時候就能保存state中的狀態。
所謂的狀態就是 state里的屬性。
class FulComponent extends StatefulWidget { const FulComponent({Key? key}) : super(key: key); @override _FulComponentState createState() => _FulComponentState(); } class _FulComponentState extends State<FulComponent> { @override Widget build(BuildContext context) { return Container(); } }
狀態
剛才講到,狀態就是state中的屬性值。下面來個示例進行講解:
class FulComponent extends StatefulWidget { const FulComponent({Key? key}) : super(key: key); @override _FulComponentState createState() => _FulComponentState(); } class _FulComponentState extends State<FulComponent> { int count = 0; static const sum = 10; final nowDate = new DateTime.now(); @override Widget build(BuildContext context) { return Container( child: Column( children: [ Text('${sum}'), ElevatedButton.icon( onPressed: () { setState(() { count++; }); }, icon: Icon(Icons.add), label: Text('添加') ) ], ), ); } }
例如 整型值count、常量 sum、當前時間。這都是屬于狀態值,它們存在的區別就是count可以通過setSatate
進行改變。
當每次執行setState()
時,此組件都會調用build方法進行將改變的數據進行重新渲染,以此來保證state中的屬性值的保存。
State生命周期
class FulComponent extends StatefulWidget { const FulComponent({Key? key}) : super(key: key); @override _FulComponentState createState() => _FulComponentState(); } class _FulComponentState extends State<FulComponent> { int count = 0; static const sum = 10; final nowDate = new DateTime.now(); @override void initState() { // 初始化生命周期鉤子 super.initState(); //初始化操作 在這里做 } @override void didChangeDependencies() { super.didChangeDependencies(); // 依賴發生變更時 執行 } @override void reassemble() { super.reassemble(); // 重新安裝時執行,一般在調試的時候用,在發正式版本時 不會執行 } @override void didUpdateWidget(covariant FulComponent oldWidget) { super.didUpdateWidget(oldWidget); // 組件發生變更時調用,當父組件有變動,子組件也會執行此方法 } @override void deactivate() { super.deactivate(); // 停用 } @override void dispose() { super.dispose(); // 銷毀 } @override Widget build(BuildContext context) { return Container( child: Column( children: [ Text('${sum}'), ElevatedButton.icon( onPressed: () { setState(() { count++; }); }, icon: Icon(Icons.add), label: Text('添加') ) ], ), ); } }
原文鏈接:https://honker.blog.csdn.net/article/details/124511565
相關推薦
- 2022-05-25 version `GLIBC_2.18‘ not found
- 2022-01-27 laravel6.2多用戶認證分不同用戶表進行驗證登錄認證
- 2022-05-08 ASP.NET?MVC使用母版頁視圖_實用技巧
- 2022-07-10 四種整型數據的表現形式以及常見的轉移字符
- 2022-04-04 git: master (pre-receive hook declined)
- 2022-03-18 .Net使用分表分庫框架ShardingCore實現多字段分片_實用技巧
- 2022-06-26 C#實現數組元素的數據類型轉換方法詳解_C#教程
- 2022-12-09 使用adb命令從電腦傳文件到手機(傳文件)_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支