Navigation from one MaterialApp to the route on second MaterialApp - flutter

So this is the case, I have two MaterialApps in a Stack widget, I made it like that so they have separate navigation routes. The problem is, I need to navigate to a page which is on MaterialApp 1 from MaterialApp2.
Even clearer example: Button is on MaterialApp 2, I need to press that button and page on MaterialApp 1 shows up. How do I do this? Whenever I use Navigator.of(context).push() on MaterialApp 2, it of course changes the page on that MaterialApp...

It's not necessary that a Navigator.push() called in a specific MaterialApp widget will be doing push operation on that app's stack. Which MaterialApp's stack to choose depend on the Buildcontext you pass in the push function.
You can do that by saving the BuildContext of both materialApps globally and using whichever you want in Navigator.push().
Wrap the children of both MaterialApps in Builder widget to extract the BuildContext of both. Say of app1 it's context1 and app2 it's context2.
Save both of them globally using static variables in a different class say "Data".
Now while calling Navigator.push() anywhere in your app, instead of simply putting that widget's context in the context parameter use the contexts of the materialApps you saved earliers.
Say you want to push a page on MaterialApp1's stack, do
Navigator.push(Data.context1,/*Rest of the builder function as it is*/);
Also instead of using two different MaterialApps just for getting two seperate navigators, you can try using the Navigator widget.

Related

is it better to use Navigator over BottomNavigationBar when we talk about performance in Flutter?

Is it better to use Navigator over BottomNavigationBar when we talk about performance? like is this "bottom navigation bar" not gonna demand more resources, since, in Navigator.pop, the route will removed from the memory, but with the BottomNavigationBar all routes will be stored in the memory and just display the index we want? am I correct?
BottomNavigationBar just executes a function when you tap on a tab—how the tabs are implemented is up to you.
With an IndexedStack, every child is built when shown, and visibility is simply toggled when the index changes.
You can also use AutomaticKeepAliveClientMixin if you want to preserve a State when changing tabs, without building them all at once.
If you want the tabs to be disposed, you don't need to use either of these. Simply build the body of the page depending on the index of the BottomNavigationBar.

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

Using Riverpod for Flutter state, what's better: ConsumerWidget or just a Consumer where needed?

I'm fairly new to Riverpod but it seems that using a ConsumerWidget as the body of a screen is a bad practice because the screen is rebuilt when not needed.
For example:
the main widget (the screen itself) is a ConsumerWidget
somewhere in the hierarchy I have a list of clickable buttons, for which I'm watching a ChangeNotifierProvider to update a selected index (only one button can be clicked at a time).
It seems that whenever I click one button to update the index (and change the color of the button), the main widget's Build method is called, along with the items in my list.
However, when using just a Consumer widget inside the itemBuilder method of my ListView, clicking one button no longer triggers the build method of the main widget.
So, is it considered a good practice to just use Consumer widgets where needed?

how to make the App bar stay across all the pages in flutter?

how to make the app bar stay persistent across all the page? I tried with creating a separate file and then importing it under drawer property but its not working...is there any other way of doing it?
from what I understand from persistent. You want that appbar to remain static when changing pages. To do that you should create and use a single scaffold for every page instead of creating a new scaffold for every page. You have to create widgets with bodies of different pages and then set those widgets to the main scaffold body using some conditions in setState. Note as you will be using setState you need to use a stateful widget.
Here is an elaborate article on how to do that with the bottom navbar.

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.