How to call a function from another page in flutter? - 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.

Related

Why "this" changes also without a new class statefull widget screen instance?

I have a screen as statefull widget. When I instance it I have the same "this" in initState and build methods, as expected.
But if, from that screen, I open a new screen and than I come back to the first screen, first the statefull widget screen doesn't appear to be reinstanced, the method initState is not called again but the method build is called (as expected, the screen has to be drawed again) and it has a new "this" than the first time the screen has been displayed.
I could also accept the screen class has a new this but how is it possibile without a new instance of the screen widget?
It seems the class change its "this" without a new instance of the class.
Is it possibile? Why? Am I wrong?
By documents of flutter's stateful initstate
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Which means when you added the stateful widget for the first time this method is called and this (instance) is created..
Now the build method is called on various situations
The framework calls this method in a number of different situations. For example:
After calling initState.
After calling didUpdateWidget.
After receiving a call to setState.
After a dependency of this State object changes (e.g., an InheritedWidget referenced by the previous build changes).
After calling deactivate and then reinserting the State object into the tree at another location.
So each timethe build is called a new instance is created if you have added the code to create an instance in build method.
When you move to the next page the first screen is still in widget tree. Then you pop screen 2 so you see screen1 ahain but this time its not added to the widget tree so only the build is called.. if you do navigator.push from screen 2 and navigate to screen 1 you will see initstate being called again because a new instance of screen1 is added to the widget tree..

Flutter calling a method inside the build method

I have a screen with two widgets, one has a button the other one has a bottom sheet function.
I want to invoke the bottom sheet function from the other widget the problem is that my bottom sheet function needs context which makes it must be invoked inside the build function. I can invoke any function using a voidcallback on widget 1 which has the button and a constructor method on widget 2 but I need context to invoke this function. How is it possible to achieve that?
just make your method accept a BuildContext context as parameter, then when you call it you can pass the context of the widget that makes the call.

Can we pass the created Stateful widget to another dart file and add that widget as body along with setState?

Can we pass the stateful widget created in one dart file to another dart file along with the setState changes and render that widget as the body in another stateful dart file.
Actually i am able to get the body but my setState is not working.
i want to pass the stateful widget(ie widget with dropdownbutton) to another page and my setState should work and i should be able to capture the data.
When you want to call the parent method, you can use state up. (Give call back to parent).
and if you want to call child method from a parent you can use GlobalKey.

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.

Set State of parent Widget

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