Stateful widget inside a Stateless widget and vice versa - flutter

How does Flutter rebuild/repaint work in case of:
Creating a Stateless widget inside a Stateful widget.
Creating a Stateful widget inside a Stateless widget.
Does a Stateless widget inside a Stateful widget render each time the Stateful widget State changes?
Can a Stateful widget change inside a Stateless widget? How does it affect the Stateless widget?

First here is the difference between stateless and stateful from the flutter documentation
A stateless widget never changes. Icon, IconButton, and Text are examples of stateless widgets. Stateless widgets subclass StatelessWidget.
A stateful widget is dynamic: for example, it can change its
appearance in response to events triggered by user interactions or
when it receives data.
For the first scenario "Creating a Stateless widget inside a Stateful widget."
when we update the state of the Stateful widget it will re-run its build function, when a stateless widget is part of that build function, flutter will check if its input data has changed or not, if not it will return the same instance of the widget, if it's changed then it will create another instance from the stateless widget and discard the previous one.
here is an illustration of the above scenario from this article
For the second scenario "Creating a Stateful widget inside a Stateless widget."
let's try to illustrate the widget tree first to visualize what's going on.
Stateless (1)
/ \
/ \
(2) Stateful Stateless (3)
if we update the state of the stateful widget (2), both (1) and (3) won't get affected, but our build function in the stateful widget will be called again.
and if the input coming for the parent (1) changes, then (1) and (2) will be rebuilt again and flutter will check if (3) has input data changes it will be also rebuilt, if not it will return the same instance.
hope this answers your question and makes you better understand how flutter renders its UI
Note: this is my current understanding of the way flutter handles rebuilding stateful and stateless widgets, feel free to correct me if anything is not correct.

Related

When do we need to rebuild the StatelessWidget?

In the Flutter job interviews, sometimes I am asked how we can rebuild the StatelessWidget and the answer to this is to call the markNeedsBuild() method of the widget's Element or BuildContext.
So the question is, are there any cases when we need to rebuild a StatelessWidget and it's better to use a StatelessWidget instead of the StatefulWidget?
I found cases where people wanted to rebuild the StatelessWidget, such as How to invoke a rebuild of a stateless widget?, but in all these cases it was better to use StatefulWidget or state management instead.
The whole purpose of the StatelessWidget is not to rebuild.
If you require rebuild you have to use StatefullWidget.
I guess the interviewers are trying to trick you xD
Summary
Stateless Widget:
Stateless Widgets are static widgets.
They do not depend on any data change or any behavior change.
Stateless Widgets do not have a state, they will be rendered once and will not update themselves, but will only be updated when external data changes.
For Example: Text, Icon, RaisedButton are Stateless Widgets.
Stateful Widget:
Stateful Widgets are dynamic widgets.
They can be updated during runtime based on user action or data change.
Stateful Widgets have an internal state and can re-render if the input data changes or if Widget’s state changes.
For Example: Checkbox, Radio Button, Slider are Stateful Widgets

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.
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.

Stateful widget with no state field

I sometimes see stateful widget with the state which doesn't have any state fields. What is the reason of using such stateful widget? Is not better in that case to convert that stateful widget into stateless widget?
State objects (created by StatefulWidget) have their own lifecycle. They can override initState, didChangeDependencies, didUpdateWidget, dispose, etc to perform some tasks at specific cases. See lifecycle description in State class docs

Flutter ListView in StateLess Widget

as I learned , in StateLessWidget we can't call any thing like setState to rebuild the widget tree and so on.
but when we scroll a listVeiw , the position and ... will change.
but as we in StatelessWidget , I don't know why it is possible to change the State of STL because the ListView has its own offset state and it can change and reflected in User Interface .
ListView is a stateful widget.
Just because you wrap it in a stateless widget does not mean it also becomes stateless.
That's the whole magic, you can have many stateful and stateless widgets wrapped in stateful and stateless widgets...

What difference between stateless and stateful widgets?

I am learning Dart/flutter and trying to understand how Widgets system works. But I can't understand what difference between stateless and stateful widgets? For example I have button. What type it have?
Let's imagine two cases.
I send text to button and it's display it.
I send text to button and it's change color.
What will be if I will create not proper Widget type?
There are 3 kind of widgets, not just 2.
Stateful widget
Stateless widget
Inherited widget
A stateless widget is like a constant. It is immutable. If you want to change what is displayed by a stateless widget, you'll have to create a new one.
Stateful widgets are the opposite. They are alive and can interact with the user. Stateful widgets have access to a method named setState, which basically says to the framework "Hello, I want to display something else. Can you redraw me please ?".
Finally, Inherited widget is a mixt of both worlds. It is immutable and stateless. But another widget (whatever it is) can subscribe to that inherited widget.
Which means that when you replace your inherited widget by a new one, all the widgets that has subscribed to the old one will be redrawn.
In the end, a stateful widget will usually be used like a Controller.
A stateless widget will be used like a View.
And the inherited widget will be your configuration file or your Model.
According to flutter.io:
Stateless widget
Stateless widgets are immutable, meaning that their properties can’t
change — all values are final.
Here is doc.
Stateful widget
Stateful widgets maintain state that might change during the lifetime
of the widget. Implementing a stateful widget requires at least two
classes: 1) a StatefulWidget class that creates an instance of 2) a
State class. The StatefulWidget class is, itself, immutable, but the
State class persists over the lifetime of the widget.
As a example if you want to change text in text widget when a button press, you have to use StatefulWidget and it will let you to change the state of a variable.
But in StatelessWidget you cannot do that because it doesn't keep state.
Read more from doc.
This tutorial will help anyone who trying to understand these two.
Check out the Flutter Interactivity Tutorial.
If your widget's build method depends entirely on its immutable constructor arguments, you should use a StatelessWidget because they're simpler. If you want to store some persistent private data that you expect to mutate over time, use a StatefulWidget and store the data on the State.