flutterdartstatefulwidgetstatelesswidget

In Flutter while building your app how do we decide when to use a StatelessWidget or StatefulWidget?


The core concept of StatelessWidget and StatefulWidget is confusing to me.


Solution

  • According to flutter documentary:

    A widget is either stateful or stateless. 
    
    If a widget can change—when a user interacts with it, for example—it’s stateful.
    
    A stateless widget never changes. Icon, IconButton, and Text are examples of stateless widgets. Stateless widgets subclass StatelessWidget.
    

    so mainly if you have something on the screen that changes when user interacts with it, you should use stateful widget for it and otherwise, you should use stateless widget.

    for example if you have a plus button on the screen and a a number on the screen which should be increased every time the user press it, you should use stateful widget to notify flutter that the text on the screen should be changed and rerendered.

    for more information you can check here.