美文网首页
State 生命周期

State 生命周期

作者: 乐狐 | 来源:发表于2022-07-02 09:08 被阅读0次
state 生命周期.png

flutter 当父节点刷新子节点时,调用子节点的didUpdateWidget ? 欢迎留言

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(
    home: InitPage(),
  ));
}

class InitPage extends StatelessWidget {
  const InitPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('点击跳转'),
      ),
      body: TextButton(
        child: const Text("跳转页面"),
        onPressed: () {
          Navigator.push(context, MaterialPageRoute(builder: (context) {
            return const CounterWidget();
          }));
        },
      ),
    );
  }
}

class CounterWidget extends StatefulWidget {
  final int initValue;

  const CounterWidget({Key? key, this.initValue = 0}) : super(key: key);

  @override
  State createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  late int _counter;
  bool _magic = false;

  //State 对象创建时候初始调用一次
  @override
  void initState() {
    super.initState();
    _counter = widget.initValue;
    print("Counter initState");
  }

  //父或祖先组件(InheritedWidget)发生变化时候调用
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print("Counter didChangeDependencies");
  }

  //多次调用重绘界面
  @override
  Widget build(BuildContext context) {
    print("Counter build");
    return Scaffold(
      appBar: AppBar(
        title: const Text('生命周期测试'),
      ),
      body: Center(
        child: Column(
          children: [
            TextButton(
              child: Text('$_counter'),
              onPressed: () {
                setState(
                  () {
                    _magic = !_magic;
                    ++_counter;
                  },
                );
              },
            ),
            TextButton(
              child: const Text('返回上一页面'),
              onPressed: () => setState(() {
                Navigator.pop(context, "");
              }),
            ),
            _magic ? const SonWidget() : const SonWidget(),
          ],
        ),
      ),
    );
  }

  //IDE 热重载调用
  @override
  void reassemble() {
    super.reassemble();
    print("Counter reassemble");
  }

  @override
  void didUpdateWidget(CounterWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    print("Counter didUpdateWidget");
  }

  //删除 State 对象调用
  @override
  void deactivate() {
    super.deactivate();
    print("Counter deactive");
  }

  //永久删除 State 对象调用
  @override
  void dispose() {
    super.dispose();
    print("Counter dispose");
  }
}

class SonWidget extends StatefulWidget {
  const SonWidget({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _SonState();
}

class _SonState extends State<SonWidget> {
  int _counter = 0;

  //State 对象创建时候初始调用一次
  @override
  void initState() {
    super.initState();
    print("Son initState");
  }

  //State 依赖关系(InheritedWidget)发生变化时候调用.一般无需重写
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print("Son didChangeDependencies");
  }

  @override
  Widget build(BuildContext context) {
    print("Son build");
    return Container(
        width: 100,
        height: 100,
        decoration: const BoxDecoration(color: Colors.red),
        child: TextButton(
          child: Text('$_counter'),
          onPressed: () => setState(() {
            _counter++;
            print("点击事件");
          }),
        ));
  }

  //IDE 热重载调用
  @override
  void reassemble() {
    super.reassemble();
    print("Son reassemble");
  }

  //flutter 3,子节点被父节点刷新时再次调用时调用?
  @override
  void didUpdateWidget(SonWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    print("Son didUpdateWidget");
  }

  //删除 State 对象调用
  @override
  void deactivate() {
    super.deactivate();
    print("Son deactive");
  }

  //永久删除 State 对象调用
  @override
  void dispose() {
    super.dispose();
    print("Son dispose");
  }
}

相关文章

  • react随记4 State&生命周期

    State&生命周期 React中组件生命周期如下图: 从上图中也可以看出,组件在构造函数中有state,也就是状...

  • Flutter中State的生命周期

    Flutter中State的生命周期 Flutter中State类(状态)一般与StatefulWidget配合使...

  • 翻译练习 react-组件和props

    状态和生命周期State and Lifecycle This page introduces the conce...

  • State & 生命周期

  • W5L2

    关于羽绒服产品生命周期的分析 The current state of the available technol...

  • React 组件渲染过程---自用

    组件的生命周期 getDefaultProps : 初始化propsconstructor:可以初始化state ...

  • Flutter第一天

    Flutter官方文档 Flutter - State类 flutter中的生命周期 Flutter-learni...

  • Flutter - State类

    写了这么多,无非就是State文档贴过来,最有感触的就两点: State 的生命周期和 StatefulWidge...

  • Flutter - State类

    写了这么多,无非就是State文档贴过来,最有感触的就两点: State 的生命周期和 StatefulWidge...

  • Taro生命周期

    Taro生命周期详解 主要介绍Taro中的9个生命周期钩子 生命周期与State 1.状态更新一定是异步的 2.同...

网友评论

      本文标题:State 生命周期

      本文链接:https://www.haomeiwen.com/subject/mmbcbrtx.html