美文网首页
flutter StatefulWidget 生命周期

flutter StatefulWidget 生命周期

作者: XH小子 | 来源:发表于2022-10-11 15:57 被阅读0次
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/scheduler.dart';
    
    class LifeCyclePage extends StatefulWidget {
      LifeCyclePage({Key? key}) : super(key: key);
    
      @override
      State<LifeCyclePage> createState() => _LifeCyclePageState();
    }
    
    class _LifeCyclePageState extends State<LifeCyclePage> {
      int _counter = 0;
      @override
      void initState() {
        super.initState();
        print("initState");
        SchedulerBinding.instance!.addPostFrameCallback((timeStamp) {
          print("timeStamp=${timeStamp.inMilliseconds}");
        });
      }
    
      @override
      void setState(VoidCallback fn) {
        super.setState(fn);
        print("setState");
      }
    
      @override
      void didChangeDependencies() {
        super.didChangeDependencies();
        print("didChangeDependencies");
      }
    
      @override
      void didUpdateWidget(covariant LifeCyclePage oldWidget) {
        super.didUpdateWidget(oldWidget);
        print("didUpdateWidget");
      }
    
      @override
      void deactivate() {
        super.deactivate();
        print("deactivate");
      }
    
      @override
      void dispose() {
        super.dispose();
        print("dispose");
      }
    
      @override
      Widget build(BuildContext context) {
        print("build");
        return Scaffold(
          appBar: AppBar(
            title: Text("生命周期"),
          ),
          body: Container(
              color: Colors.white,
              alignment: Alignment.center,
              child: SubLifeCyclePage(
                counter: _counter,
                callback: () {
                  setState(() {});
                },
              )),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              _counter++;
              setState(() {});
            },
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }
    
    class SubLifeCyclePage extends StatefulWidget {
      final int counter;
      final VoidCallback? callback;
      SubLifeCyclePage({Key? key, required this.counter, this.callback})
          : super(key: key);
    
      @override
      State<SubLifeCyclePage> createState() => _SubLifeCyclePageState();
    }
    
    class _SubLifeCyclePageState extends State<SubLifeCyclePage> {
      int _counter = 0;
      @override
      void initState() {
        super.initState();
        print("initState");
        SchedulerBinding.instance!.addPostFrameCallback((timeStamp) {
          print("SubLifeCyclePage-timeStamp=${timeStamp.inMilliseconds}");
        });
      }
    
      @override
      void setState(VoidCallback fn) {
        super.setState(fn);
        print("SubLifeCyclePage-setState");
      }
    
      @override
      void didChangeDependencies() {
        super.didChangeDependencies();
        print("SubLifeCyclePage-didChangeDependencies");
      }
    
      @override
      void didUpdateWidget(covariant SubLifeCyclePage oldWidget) {
        super.didUpdateWidget(oldWidget);
        print("SubLifeCyclePage-didUpdateWidget");
        _counter = widget.counter;
      }
    
      @override
      void deactivate() {
        super.deactivate();
        print("SubLifeCyclePage-deactivate");
      }
    
      @override
      void dispose() {
        super.dispose();
        print("SubLifeCyclePage-dispose");
      }
    
      @override
      Widget build(BuildContext context) {
        print("SubLifeCyclePage-build");
        return Column(
          children: [
            Container(
              child: Text(
                "$_counter",
                style: TextStyle(color: Colors.black, fontSize: 30),
              ),
            ),
            CupertinoButton(
                child: Text("新增"),
                onPressed: () {
                  _counter++;
                  setState(() {});
                  if (widget.callback != null) {
                    widget.callback!();
                  }
                })
          ],
        );
      }
    }
    
    

    进入life cylcle 页面

    flutter: initState
    flutter: didChangeDependencies
    flutter: build
    flutter: initState
    flutter: SubLifeCyclePage-didChangeDependencies
    flutter: SubLifeCyclePage-build
    flutter: timeStamp=229580
    flutter: SubLifeCyclePage-timeStamp=229580
    

    点击+号

    flutter: setState
    flutter: build
    flutter: SubLifeCyclePage-didUpdateWidget
    flutter: SubLifeCyclePage-build
    
    

    点击新增

    flutter: SubLifeCyclePage-setState
    flutter: setState
    flutter: build
    flutter: SubLifeCyclePage-didUpdateWidget
    flutter: SubLifeCyclePage-build
    

    退出页面

    flutter: deactivate
    flutter: SubLifeCyclePage-deactivate
    flutter: SubLifeCyclePage-dispose
    flutter: dispose
    

    相关文章

      网友评论

          本文标题:flutter StatefulWidget 生命周期

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