How to pass data child to child widget without rebuild parent widget in flutter? - flutter

I have one ParentWidget having two child widget.
I want to pass some callback action from FirstChild() to SecondChild() Widget without rebuild ParentWidget()like setState((){}).
I want to do because ParentWidget() has many widget. And this callback action continuously happen.
And continuously setState((){}) is not viable option for ParentWidget()
For example
setState((){}) call from FirstChild() than I want to rebuild SecondChild() without rebuild ParentWidget()

StatefulWidget is only useful in very simple business logic cases. I highly recommend that you have a look at Riverpod, where you can access a "state" from any widget, regardless of who its parent widget is. And because of how Riverpod is designed, it will not trigger any unnecessary rebuilds.
Sticking with StatefulWidget, you will need to use something like InheritedWidget as a parent of both widgets, and then have them both access it.

You can get help with state management solutions like Provider or Riverpod.
I may write an example code if you use Riverpod, but generally speaking, in such case, you need to place the state outside the Parent widget into some external variable. Then you can access and share it between both Children without rebuilding Parent.

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

Does Inherited Widget changes rebuild whole application when it's the parrent of MaterialApp?

I want to use InheritedWidget to access and change its data from anywhere in application.
I've read many articles about InheritedWidget, but I do not understand one of its behaviors.
Here it says that only widgets that are using InheritedWidget get rebuilt when data changes(which is exactly what I want).
But here it says that InheritedWidget is immutable and its data only changes when it is rebuilt itself!
So doesn't this make the whole widget tree below the InheritedWidget get rebuilt when the data changes?
How can I wrap the MaterialApp widget with InheritedWidget so I can change its data from anywhere in app and only rebuild a small Widget that is using InheritedWidget when the data changes?
I know I can implement this using provider package very easily, but in this part of application I want to use InheritedWidget if it's possible :)
An object or widget being immutable does not mean its values can't change.
You can declare a final List<widget> children, which is immutable since it is final, and still add and remove widgets from it. What is immutable is the reference.
When you use the InheritedWidget is the same. If a value changes it wont rebuild itself and all the tree below. Even it is actually changed and rebuilt, it does not necessarily rebuild all tree below, it'd happen only if the widget type or its child/children reference changed also.

Why do I need an InheritedWidget in Flutter

I've started to learning about Flutter's inherited widgets and i have a question.
Why do we need an InheritedWidget if we can get data from context.findAncestorOfExactType?
And that updateShouldNotify... Like, it notifies child widgets when some condition is true, and they are anyway running their build methods after InheritedWidget changed, because it's immutable and we can only chage it in some rebuild...
InheritedWidget is more useful for build method cases. When you want to get data from an ancestor inside your build method.
From the Flutter API: https://api.flutter.dev/flutter/widgets/BuildContext/findAncestorStateOfType.html
This should not be used from build methods, because the build context
will not be rebuilt if the value that would be returned by this method
changes. In general, dependOnInheritedWidgetOfExactType is more
appropriate for such cases. This method is useful for changing the
state of an ancestor widget in a one-off manner, for example, to cause
an ancestor scrolling list to scroll this build context's widget into
view, or to move the focus in response to user interaction.
Also by doing what you mention, you are coupling Widgets together which is not a good practice at all. Widgets are known to be able to moved from place to place without much modification.
There are many useful things that come from the Inherited Widget. Some of these things are very useful packages, I recommend that you take a look at them.
Provider package
InheritedWidget & InheritedModel
Redux
BLoC / Rx
GetIt
You can also take a look at the InheritedWidget documentation

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.

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.