본문 바로가기
📱Mobile/🔥Flutter

[Flutter] Flutter 생명주기 Sample Code

by 요즘이슈야 2020. 10. 25.
반응형

 

main.dart

import 'package:flutter/material.dart';

void main() => runApp(TestApp());

class TestApp extends StatelessWidget {
  TestApp() {
    print('TestApp TestApp() start');
    print('TestApp TestApp() done');
  }

  @override
  Widget build(BuildContext context) {
    print('TestApp build() start');
    print('TestApp build() done');
    return MaterialApp(
      title: 'StatefulWidget LifecycleApp',
      home: _FirstStatefulWidget(),
    );
  }

}

class _FirstStatefulWidget extends StatefulWidget {
  _FirstStatefulWidget() {
    print('_FirstStatefulWidget _FirstStatefulWidget()');
  }

  @override
  _FirstStatefulWidgetState createState(){
    print('_FirstStatefulWidgetState createState() start');
    print('_FirstStatefulWidgetState createState() done');
    return _FirstStatefulWidgetState();
  }
}

class _FirstStatefulWidgetState extends State<_FirstStatefulWidget> {
  int _counter;

  _FirstStatefulWidgetState() {
    print('_FirstStatefulWidgetState _FirstStatefulWidgetState() start');
    print(
        '_FirstStatefulWidgetState _FirstStatefulWidgetState() ${this.mounted}');
    print('_FirstStatefulWidgetState _FirstStatefulWidgetState() done');
  }

  @override
  Widget build(BuildContext context) {
    print('_FirstStatefulWidgetState build()');
    return Scaffold(
      appBar: AppBar(
        title: Text('_FirstStatefulWidgetState'),
      ),
      body: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              FlatButton(
                child: Text('Go Next'),
                onPressed: () {
                  next();
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              FlatButton(
                child: Text('Counter'),
                onPressed: () {
                  _onClick();
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('$_counter'),
            ],
          ),
        ],
      ),
    );
  }

  @override
  void initState() {
    print('_FirstStatefulWidgetState initState() start');
    super.initState();
    _counter = 0;
    print('_FirstStatefulWidgetState initState() ${this.mounted}');
    print('_FirstStatefulWidgetState initState() done');
  }

  @override
  void reassemble() {
    print('_FirstStatefulWidgetState reassemble() start');
    super.reassemble();
    print('_FirstStatefulWidgetState reassemble() ${this.mounted}');
    print('_FirstStatefulWidgetState reassemble() done');
  }

  @override
  void didChangeDependencies() {
    print('_FirstStatefulWidgetState didChangeDependencies() start');
    super.didChangeDependencies();
    print('_FirstStatefulWidgetState didChangeDependencies() ${this.mounted}');
    print('_FirstStatefulWidgetState didChangeDependencies() done');
  }

  @override
  void deactivate() {
    print('_FirstStatefulWidgetState deactivate() start');
    super.deactivate();
    print('_FirstStatefulWidgetState deactivate() ${this.mounted}');
    print('_FirstStatefulWidgetState deactivate() done');
  }

  @override
  void dispose() {
    print('_FirstStatefulWidgetState dispose() start');
    super.dispose();
    print('_FirstStatefulWidgetState dispose() ${this.mounted}');
    print('_FirstStatefulWidgetState dispose() done');
  }

  @override
  void didUpdateWidget(_FirstStatefulWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('_FirstStatefulWidgetState didUpdateWidget() start');
    print('_FirstStatefulWidgetState didUpdateWidget() ${this.mounted}');
    print('_FirstStatefulWidgetState didUpdateWidget() done');
  }

  void _onClick() {
    print('_FirstStatefulWidgetState _onClick() start');
    print('_FirstStatefulWidgetState _onClick() ${this.mounted}');
    if (this.mounted) {
      setState(() {
        print('_FirstStatefulWidgetState setState() start');
        _counter++;
        print('_FirstStatefulWidgetState _onClick() ${this.mounted}');
        print('_FirstStatefulWidgetState setState() done');
      });
    }
    print('_FirstStatefulWidgetState _onClick() ${this.mounted}');
    print('_FirstStatefulWidgetState _onClick() done');
  }

  void next() {
    print('_FirstStatefulWidgetState next() start');
    print('_FirstStatefulWidgetState next() ${this.mounted}');
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (BuildContext context) => _SecondStatefulWidget(),
      ),
    );
    print('_FirstStatefulWidgetState next() ${this.mounted}');
    print('_FirstStatefulWidgetState next() start');
  }
}

class _SecondStatefulWidget extends StatefulWidget {
  _SecondStatefulWidget() {
    print('_SecondStatefulWidget _SecondStatefulWidget()');
  }

  @override
  _SecondStatefulWidgetState createState(){
    print('_SecondStatefulWidgetState createState() start');
    print('_SecondStatefulWidgetState createState() done');
    return _SecondStatefulWidgetState();
  }
}

class _SecondStatefulWidgetState extends State<_SecondStatefulWidget> {
  int _counter;

  _SecondStatefulWidgetState() {
    print('_SecondStatefulWidgetState _SecondStatefulWidgetState() start');
    print(
        '_SecondStatefulWidgetState _SecondStatefulWidgetState() ${this.mounted}');
    print('_SecondStatefulWidgetState _SecondStatefulWidgetState() done');
  }

  @override
  Widget build(BuildContext context) {
    print('_SecondStatefulWidgetState build()');
    return Scaffold(
      appBar: AppBar(
        title: Text('_SecondStatefulWidgetState'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                    child: Text('Go Back'),
                    onPressed: () {
                      back();
                    }),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                    child: Text('Counter'),
                    onPressed: () {
                      _onClick();
                    }),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('$_counter'),
              ],
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    print('_SecondStatefulWidgetState initState() start');
    super.initState();
    _counter = 0;
    print('_SecondStatefulWidgetState initState() ${this.mounted}');
    print('_SecondStatefulWidgetState initState() done');
  }

  @override
  void reassemble() {
    print('_SecondStatefulWidgetState reassemble() start');
    super.reassemble();
    print('_SecondStatefulWidgetState reassemble() ${this.mounted}');
    print('_SecondStatefulWidgetState reassemble() done');
  }

  @override
  void didChangeDependencies() {
    print('_SecondStatefulWidgetState didChangeDependencies() start');
    super.didChangeDependencies();
    print('_SecondStatefulWidgetState didChangeDependencies() ${this.mounted}');
    print('_SecondStatefulWidgetState didChangeDependencies() done');
  }

  @override
  void deactivate() {
    print('_SecondStatefulWidgetState deactivate() start');
    super.deactivate();
    print('_SecondStatefulWidgetState deactivate() ${this.mounted}');
    print('_SecondStatefulWidgetState deactivate() done');
  }

  @override
  void dispose() {
    print('_SecondStatefulWidgetState dispose() start');
    super.dispose();
    print('_SecondStatefulWidgetState dispose() ${this.mounted}');
    print('_SecondStatefulWidgetState dispose() done');
  }

  @override
  void didUpdateWidget(_SecondStatefulWidget oldWidget) {
    print('_SecondStatefulWidgetState didUpdateWidget() start');
    super.didUpdateWidget(oldWidget);
    print('_SecondStatefulWidgetState didUpdateWidget() ${this.mounted}');
    print('_SecondStatefulWidgetState didUpdateWidget() done');
  }

  void _onClick() {
    print('_SecondStatefulWidgetState _onClick() start');
    print('_SecondStatefulWidgetState _onClick() ${this.mounted}');
    if (this.mounted) {
      setState(() {
        print('_SecondStatefulWidgetState setState() start');
        _counter++;
        print('_SecondStatefulWidgetState _onClick() ${this.mounted}');
        print('_SecondStatefulWidgetState setState() done');
      });
    }
    print('_SecondStatefulWidgetState _onClick() ${this.mounted}');
    print('_SecondStatefulWidgetState _onClick() done');
  }

  void back() {
    print('_SecondStatefulWidgetState back() start');
    print('_SecondStatefulWidgetState back() ${this.mounted}');
    Navigator.pop(context);
    print('_SecondStatefulWidgetState back() ${this.mounted}');
    print('_SecondStatefulWidgetState back() done');
  }
}
반응형