Flutter有无状态类与State及生命周期详细介绍

作者:聂大哥 时间:2022-12-27 15:48:53 

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生命周期

Flutter有无状态类与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

标签:Flutter,状态类,State,生命周期
0
投稿

猜你喜欢

  • Android开发签名知识梳理总结

    2023-03-15 03:52:02
  • C# winform循环播放多个视频

    2021-06-30 06:39:21
  • SpringBoot使用swagger生成api接口文档的方法详解

    2021-10-22 18:11:48
  • 基于C语言实现井字棋游戏

    2023-06-28 21:23:18
  • C#中执行批处理文件(*.bat)的方法代码

    2022-12-01 10:25:01
  • C# winfrom 模拟ftp文件管理实现代码

    2023-07-15 16:29:48
  • Java多线程回调方法实例解析

    2023-11-04 01:40:01
  • 利用Lambda表达式创建新线程案例

    2023-08-26 23:16:00
  • 关于maven打包时的报错: Return code is: 501 , ReasonPhrase:HTTPS Required

    2022-09-09 00:50:51
  • SpringBoot内部调用事务不起作用问题的解决方案

    2023-11-17 13:38:35
  • 解决Spring或SpringBoot开启事务以后无法返回自增主键的问题

    2023-09-12 18:51:45
  • SpringBoot整合MyBatisPlus配置动态数据源的方法

    2021-07-31 04:01:59
  • Java 多线程Synchronized和Lock的区别

    2023-08-14 11:39:09
  • Kotlin语言使用WebView示例介绍

    2021-10-14 05:56:21
  • Android在JNI中使用ByteBuffer的方法

    2021-11-08 21:14:35
  • 使用C#实现在屏幕上画图效果的代码实例

    2022-09-11 05:12:32
  • Java Bean与xml互相转换的方法分析

    2021-08-12 13:34:00
  • Java即将引入新对象类型来解决内存使用问题

    2023-03-30 18:12:42
  • Android更多条目收缩展开控件ExpandView的示例代码

    2022-07-31 18:10:24
  • Java split函数拆分后变成null问题解决方案

    2022-05-24 17:00:36
  • asp之家 软件编程 m.aspxhome.com