Stateful Widget Lifecycle and Stateless Widgets

Stateless widget life cycle

 

The stateless widget does not change over time, so the build method is called once.

 

Example:

 

class MyWidget extends StatelessWidget {

  const MyWidget({Key? key}) : super(key: key);


  @override

  Widget build(BuildContext context) {

    return Container();

  }

}

 

Stateful widget life cycle

 

Stateful Widgets are those widgets that hold the State and the user interface being described can change dynamically at runtime.

 

Example:

 

class MyWidget extends StatefulWidget {

  const MyWidget({Key? key}) : super(key: key);


  @override

  State<MyWidget> createState() => _MyWidgetState();

}


class _MyWidgetState extends State<MyWidget> {

  @override

  Widget build(BuildContext context) {

    return Container();

  }

}

 

Stateful widget life cycle

 

  • createState ()
  • initState ()
  • didChangeDependencies ()
  • build (
  • didUpdateWidget)
  • setState ()
  • deactivate ()
  • dispose ()

  • createState ()

 

When Flutter is instructed to build a StatefulWidget, it immediately calls createState(). It will return an instance of a State associated with it.

 

Example:

 

class MyWidget extends StatefulWidget {

  const MyWidget({Key? key}) : super(key: key);


  @override

  State<MyWidget> createState() => _MyWidgetState();

}

 

  • initState ()

 

This is the first method called when the widget is created. initState is called once and only once.

 

Example:

 

@override

  void initState() {

    super.initState();

    // TODO

  }

 

  • didChangeDependencies ()

 

This method is called immediately after initState on the first time the widget is built.

 

Example:

 

@override

  void didChangeDependencies() {

    // TODO

    super.didChangeDependencies();

  }

 

  • build ()

 

It is a required and overridden method which is triggered every time a widget is rebuilt. The build function is used to build the widget tree. It is called more than one time, when the state of our app changes then it is called. This can be after a call to initState(), didChangeDependencies), didUpdate Widget() or when the state is changed via call to setState().

 

Example:

 

@override

  Widget build(BuildContext context) {

    return Container();

  }

 

  • didUpdate Widget ()

 

This method is used when there is some change in the configuration by the Parent widget. It is called every time we hot reload the app for viewing the updates made to the widget. A typical case is when a parent passes some variable to the children() widget via the constructor.

 

Example:

 

@override

  void didUpdate Widget (Home oldWidget) {

    super.didUpdate Widget(oldWidget);

    // TODO

  }

 

  • setState ()

 

The setState() method is called often by the Flutter framework itself and the developer. It is used to notify the framework that "data has changed", and the widget at this build context should be rebuilt.

 

Example:

 

setState (() {

    // setState implementation

  });

 

  • deactivate ()

 

Deactivate is called when State is removed from the tree, but it might be reinserted before the current frame change is finished.

 

This method exists basically because State objects can be moved from one point in a tree to another.

 

Example:

 

@override

  void deactivate() {

    super.deactivate ();

    // TODO

  }

 

  • dispose():

 

It is called when the object and its State needs to be removed from the Widget Tree permanently. This method is where to unsubscribe and cancel all animations, streams, etc.

 

Example:

 

@override

void dispose () {

super.dispose ():

// TODO

}


Video Link:

Post a Comment (0)
Previous Post Next Post