How to use Stateful Builder

Now that you have been introduced to StatefulBuilder , you may be wondering how it is used.

StatefulBuilder is best used in situations where you have a medium/large widget tree and state needs to be introduced for a small subsection of that tree.

The above code snippet shows StatefulBuilder being used in a Card . Instead of making the entire card stateful, we are using StatefulBuilder to build the section of the tree where state is needed. In this case, we are using it to only build the Row of buttons and text. As a result of this, when onPressed is triggered and setState is called, only the Row and its children are rebuilt, all other widgets are not.

If the entire widget was made stateful, instead of having only the Row rebuilt, the entire Card would have been rebuilt.

Fig 1: Diagram showing only the entire widget marked as being rebuilt

Video of finished app: