Set State of parent Widget - flutter

Let's say, I have a custom button widget and I want to set the state of it's parent using setState. How exactly can I access the parent widget's state from my custom button? Is it even possibile?

You can use callbacks functions to achieve this. You can refer here.
In the link
FeedPage is similar to CustomButton(ChildWiget)
RootPage is similar to ParentWiget

Related

How to pass data child to child widget without rebuild parent widget in 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.

How to call a function from another page in flutter?

So I have a call a timer function on pressing the login button in the main file. The timer function is defined in the login verification file. So how can I call the timer function in main file so that when the login button is pressed and when it goes to the login verification the timer starts?
If you want to use some parent's function in child widget then it is easy - just pass the reference to the function you want through the constructor.
If you want to use some child's function in parent widget then the situation is a little bit different - you have to pass a GlobalKey to identify the state of child widget when creating an instance of this widget. You can refer to this https://stacksecrets.com/flutter/how-to-call-method-of-a-child-widget-from-parent-in-flutter.
If you want to use a function from a widget that is not either a child or parent widget, then you might think of using some state managment solution (such as Provider or GetX or Bloc) to provide it to all the widget that needs some information from it.

How to refresh or call set state for a particular container widget in flutter

In my project i have different container widgets that are placed one above the another in a stack layout and i have performed set of canvas drawing on each container widget based on my scenario. My question is "how to refresh or call set state for a particular container widget without rendering all the widgets"?.
Thanks in advance,
Ashwin
You will need transform your "Container" in a custom statefull widget and then make his state management by your needs. Take a look at BLoC pattern here. With this pattern you will be able to update a specific widget in all widget tree without recreate all widgets.

Flutter - Call a method on some children from parent

I'm discovering Flutter and I really love it.
I'm building a simple app where I have few instances of Statefull custom widget "Counter" on a Scafold's body, and I have a "Reset" button on the drawer.
What's the best way to call a "resetCounter" method on all "Counter" instances when tap on the "Reset" button ?
I've managed the other way (calling a callback method of a parent from a child), but I can't find the other way. The only solution I've found is by using GlobalKeys, but it doesn't seems appropriate.
Thanks
Indeed for your case callback function might be the right one. But when your app will grow the widget hierarchy will become more and more complex. For now you have Parent —> Child relationship and it’s quite easy to pass callback function, but when you’ll get Parent —> Widget A—> Widget B—> Child situation in order to pass callback from Parent to Child you’ll need to pass it through Widget A and Widget B as well.
For me, your problem sounds like a perfect candidate for reactive streams. You may create a Sink which will be used when user tap on Reset button and your Counter will observe this event and handle it with resetCounter method. You may learn more about reactive programming (and other ways to solve your problem) from this presentation.
There is another approach to extend ChangeNotifier, and add child as listeners. Once parent perform an action, you can call notifyChange to trigger child widgets callbacks.

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.