Flutter: When to create Stateless or Stateful page? - flutter

I am new to Flutter. Everything in Flutter is a widget, and there are two types of widgets, which are Stateless and Stateful. Understood that stateless widgets are widgets that will not change or user can't interact with (texts, icons, etc) while stateful widgets are widgets that will change its state for example because of user interactions.
When we want to create a new custom page we usually extends the page from StatelesWidget or StatefulWidget. Since a StatelesWidget can have StatefulWidget as its children and vice-versa, then when should we extend a page as a StatefulWidget or as a StatelessWidget?
Thank you.

If the page itself has some kind of status, then it should be a stateful widget. For example you want to load something remotely, and display a progress indicator while data is being fetched. When loading is complete, the state of the page is changed, and instead of the progress indicator you display whatever you want.
But it is also possible that the page itself is a stateless widget, and has a child widget, a container for example, and this container is stateful, managing the above mentioned remote loading or depends on some kind of user interaction.
State management is a central issue in Flutter, you have many options, and it is not always easy to find the best. You can easily get into fighting the framework instead of letting it to do the job for you. If you are new to this, I would suggest watching some videos, they helped me a lot, for example this or this.

Related

Why use BloC or Provider in Flutter when we already have Flutter's built-in setState?

Maybe I don't understand the purpose of BloC or Provider but I'm confused as to why we would ever want to use them instead of using Flutter's built-in state management using the Stateful widget. I've just finished an app and can't remember a point where I wished I needed something more than the defaults. Can anyone clear things up for me?
There are a few reasons to use a BloC or Provider rather than Flutter's built-in setState:
BloC and Provider offer a more robust way to manage state.
BloC and Provider make it easier to update state across multiple widgets.
BloC and Provider can be used to manage async data.
BloC and Provider offer a more modular way to structure your code.
There are some case that you need BLoC to make you easier to define each state or condition that happening inside the application.
We will start discussing creating app like https://www.tokopedia.com/ (inspect element and go with phone size preview). You will see that between widget section tokopedia_ss there are some loading animations, and when the data load complete the widget loading animation changed to viewable widget (as user).
in bloc, you will make stateLoading(), stateComplete(data), stateFailed(data). and in the controller or screen you can describe what will happen when bloc state is stateLoading etc...
Creating this case with setstate is more complicated and make your code messy, also setstate will make phone render all the Build() code. not like BloC builder, you can define each widget or section.
So, when there are 10 sections, using Bloc you able to make it render each state but when using standard setstate it will render all the 10 sections in one time every state changes.
More information about BloC: article_about_BloC

When using a ListView in flutter to retrieve a list of json objects from an API. Is it the right way to have an Stateful or Stateless Widget?

So say I have a simple app with 2 routes. The home route is profile and the second is items. So the idea is that when the user clicks in the item icon the app should move to that route and display all the user's items. The question is should this item Widget be Stateless or Stateful? Take in consideration that I expect the user to pull to refresh this Route and reload the api in the Listview.
It should definitely be a Stateful widget since you are expecting data from an API. A stateless widget can't change, you should only use a Stateless widget if the data on the page is static.
I know you already have an accepted answer which is correct, but wanted to add that generally if you use a state management solution like Provider, Bloc, GetX etc...they all provide widgets that will refresh a ListView without needing to be inside a stateful widget.
That will be a cleaner and more scalable solution than calling setState from within a stateful widget.
Its definitely an stateful widget because stateless widget can't change their state.

Flutter - Two stateful in one stateless or two stateless in one stateful

I'm quite new to Flutter and I struggle to understand at which stage I should use Stateful widgets. To illustrate my issue, here is a simple layout.
Simple Layout
Let assume that we want to change the background color of the containers when they are tapped by the user.
Should I make my HomePage widget a Stateful widgets holding the state of the containers ? Or should I make my HomePage a Stateless widget and make my two containers stateful widgets holding their state ?
First, you need to understand when you need to use Stateful Widget. Flutter offical website says that
Stateful widgets are useful when the part of the user interface you are describing can change dynamically, e.g. due to having an internal clock-driven state, or depending on some system state.
You probably want to change the state of one Container at a time when tapped, not the the whole HomePage. Therefore, if you make HomePage Stateful Widget, you will have to re-render both Containers when one of them tapped. If you make both Containers Stateful Widget separately and HomePage Stateless Widget, only the Container which is tapped will be rebuilt. So, the benefits of choosing one over another change depending on your goal. Choose the one works better for you.

Why do we need stateless widgets if the same can be achieved by stateful widgets in flutter?

I am a newbie in the world of flutter, and I recently learned (or I think I did) about stateful and stateless widgets which is kind of the base for flutter widgets.
We use stateless widgets for things that are not redrawn on the display, (like text, button etc.) but stateful widgets can redraw themselves.
So my question is why do we need stateless widgets if stateful widgets can be used to draw the same kind of widgets that a stateless widget can?
Or is there any specific reasons to use stateless over stateful widgets in flutter? Or can we use stateful widgets all the time rather than stateless widgets which can draw content only once?
Thanks, and sorry if this is a stupid question.
EDIT
Well the question is not the difference between stateless and stateful.
I know the difference but what is the impact of using only stateful widgets since by using it we could also implement most of the things a stateless widget can do then why do we need stateless widgets?what's the importance of it in a flutter environment were most of the apps will be re-drawn time-to-time?
From their documentation:
Stateless widget are useful when the part of the user interface you are describing does not depend on anything other than the configuration information in the object itself and the BuildContext in which the widget is inflated. (= use when you don't need to "update the UI here").
Stateful widgets are more resource consuming and you always need to think about performance.
Here is more about this.
Push the state to the leaves. For example, if your page has a ticking
clock, rather than putting the state at the top of the page and
rebuilding the entire page each time the clock ticks, create a
dedicated clock widget that only updates itself.
Even more on this :)
I hope this answers your question.
Yes, StatefulWidget can rebuild. That happens typically when using Inheritedwidgets.
StatelessWidget exists to split a big widget tree into smaller reusable widgets.
You might think "but I can use StatefulWidget or functions for this". Which is true, but not exactly:
StatefulWidget comes with a huge boilerplate, which you do not need in that situation. So this just adds noise and makes your code less readable.
Functions cannot rebuild independently, nor to they have access to key and override ==. So they can be less performant or introduce bugs.
Every time we use a Stateful widget, a state object is created. If we use all Stateful widgets, there will be a lot of unnecessary state objects which will consume a lot of memory. So where we don't need to change the state, we should use the simpler one - the Stateless widget.
Another reason to use Stateless widgets over Stateful widgets is Stateful widget comes with a huge boilerplate and according to Flutter API Documentation using a bunch of nested Stateful widgets, passing data through all those build methods and constructors can get cumbersome.
This is what I understand ...
When you use a stateful widget and redraw it, all other widgets inside will be redrawn as well. So we try to use stateless widgets so as not to redraw the other widgets within them, but you know we generally need to change the data on the screen and it should happen inside a "single widget" and this widget should be the one stateful widget to use as little computing power as possible.
Now ... I guess you're thinking: "but what if i just use stateful widgets and don't redraw them?" well, as you know, when you use a stateful widget you have two classes: widget and state. I have gotten that when you declare a state you tell the phone to keep and save this state in memory whether the widget is redrawn or not, so you use the phone memory for no reason because you don't need to redraw its widget.
We should think about always using stateless widgets because they are lighter than stateful widgets and we should always make our stateful widgets the smallest in our application to redraw as few widgets as possible in the widget tree of the application.
I hope I helped a little.
Theoretically I think you can use stateful widgets every where, but your overall program would be heavier and would have a lot of redundancies.
That said, I don't think there is any advantage of using stateless widgets over stateful widgets.

Should screens be Stateless or Stateful?

I'm developing an app with lots of screens and pages. I've read somewhere that you should use Stateless Widgets whenever you can.
Why is that?
If I have a lot of screens, should those be stateless? And then the content inside be Stateful? Is it better to have both the screen and widgets inside being Stateful?
You should ask yourself some questions about the screen/page to decide if it will be Stateless or Stateful.
The most obvious, does it need to change state?
Do you need to call initState, didChangeDependencies or another lifecycle method?
Is a bad practice to make Stateful when not needed. A good idea might be to start always as Stateless widget and if needed you can change it to Stateful easily with Alt + Enter shortcut (Android Studio).
I always start by creating a Stateless Widget and work with it until I have to change something state. So I could quickly use `Alt-Enter/ Convert to a Statefull from Intellij/AS to change it to stateful. (doing the inverse is not so easy, so...).
Moreover, if you use Stateful widget with some async mechanism, like streams, you could build the widget once and use the streams to update the information you need, and it will not impact the performance of your app so much. But if you call setState many times, this can degrade your App, since for every setState the Widget tree will be rebuilt.
This article from the flutter docs shows interesting tips about handling state change in flutter apps: