You are currently viewing Flutter Stateless and Stateful widget Tutorial

Flutter Stateless and Stateful widget Tutorial

What is a StatelessWidget?

A StatelessWidget is a widget that does not require mutable state.

StatelessWidget describes part of the user interface by building a constellation of other widgets that describe the user interface more concretely.

Here's the api definition of StatelessWidget:

abstract class StatelessWidget extends Widget

StatelessWidget is contained in the flutter package.

StatelessWidgets will build the it's constellation of concrete widgets recursively until the description of the user interface is fully concrete (e.g., consists entirely of RenderObjectWidgets, which describe concrete RenderObjects).

You will use Stateless widgets when the part of the user interface you are creating does not depend on anything other than the configuration information in the object itself and the BuildContext in which the widget is inflated.

However if you have compositions that will change dynamically, e.g. due to having an internal clock-driven state, or depending on some system state, then use StatefulWidget.

StatelessWidget has a build() method that will be called only in three situations:

  1. The first time the widget is inserted in the tree.
  2. When the widget's parent changes its configuration.
  3. When an InheritedWidget it depends on changes.

This positively impacts performance.

If a widget's parent will regularly change the widget's configuration, or if it depends on inherited widgets that frequently change, then it is important to optimize the performance of the build method to maintain a fluid rendering performance.

Stateless Widget example

The following is a skeleton of a stateless widget subclass called GreenFrog:

class GreenFrog extends StatelessWidget {
  const GreenFrog({ Key key }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return new Container(color: const Color(0xFF2DBD3A));
  }
}

Normally widgets have more constructor arguments, each of which corresponds to a final property. The next example shows the more generic widget Frog which can be given a color and a child:

class Frog extends StatelessWidget {
  const Frog({
    Key key,
    this.color: const Color(0xFF2DBD3A),
    this.child,
  }) : super(key: key);
  final Color color;
  final Widget child;
  @override
  Widget build(BuildContext context) {
    return new Container(color: color, child: child);
  }
}

What is StatefulWidget?

A StatefulWidget is a widget that has mutable state.

Technically it's an abstract class and is one of the classes that directly derive from the also abstract Widget class.

abstract class StatefulWidget extends Widget

It's defined in the flutter package:

It's unique feature is the fact it has mutable state while it's sister the StatelessWidget doesn't.

Basically the State refers to information that

  1. can be read synchronously when the widget is built.
  2. might change during the lifetime of the widget.

Stateful widgets will describe part of a user interface by building a constellation of other widgets that describe the user interface more concretely. The building process continues recursively until the description of the user interface is fully concrete (e.g., consists entirely of RenderObjectWidgets, which describe concrete RenderObjects).

These widgets(Stateful) are important when the part of the user interface you are describing can change dynamically.

If you are creating interfaces that rely only on the configuration information in the object itself and the BuildContext in which the widget is inflated, consider using StatelessWidget.

StatefulWidget instances themselves are immutable and store their mutable state either in separate State objects that are created by the createState method, or in objects to which that State subscribes.

The framework will call createState whenever it inflates a StatefulWidget, which means that multiple State objects might be associated with the same StatefulWidget if that widget has been inserted into the tree in multiple places. Similarly, if a StatefulWidget is removed from the tree and later inserted in to the tree again, the framework will call createState again to create a fresh State object, simplifying the lifecycle of State objects.

A StatefulWidget keeps the same State object when moving from one location in the tree to another if its creator used a GlobalKey for its key.

Leave a Reply