How to update StatefulWidget from other class? - flutter

First of all, I'm a real beginner with Flutter, so I have probably not even grasped the basic design patterns yet. My problem is probably simple for someone who understands flutter. Here goes:
I have a tabbed GUI in my app and a floating action button. Each tab has a list that I want to update using that action button (FAB). The easy way to do this seemed to have one action button for each tab, but I read that you should not do this in flutter. So I made the button "global" and call a method on the current tab when the button is pressed. The method then adds some data that should be shown in the list, but the problem is that the list of course isn't actually updated just because I add data. The setState() is never called inside that list widget.
All the exampels I've found are very simple with the FAB being inside the list having access to its setState() method. But in my case I'm outside the list. How do I solve this? Notifier? Stream? I'm lost...

I think the correct solution is to use a "Stack" and "Positioned" widged like:
- Stack
- Scaffold
- TabBar
- Positioned
- FloatingActionButton

Related

Use Riverpod to disable a button if textfields are empty

I'm struggling to figure out how to use Riverpod for the following scenario.
I have a file called textformfields.dart with 3 TextFormFields
I have another file called buttons.dart (which is widely used in the whole project) which generates an ElevatedButton
I have another file called submitProject.dart which returns a Scaffold and calls the textformfields.dart and the buttons.dart
My goal is to disable the button, if at least one of the 3 TextFormFields is empty.
I really have no idea how to start, since I'm not really familiar with State Management and Riverpod yet.
Can you give me some tips?

ShowSearch function rebuilds the whole widget tree on Flutter

I have a problem when i navigate to multiple pages and then show search.
More specifically, i have a product page with some details. At the bottom there is a list of similar products.
The flow is:
open many similar products (so I navigate to the same screen using Navigator pushNamed).
then, navigate from the las product page to the search page
tap on search bar, open search delegate using the showSearch function
My issue is that when i tap on the search bar, then the whole widget tree is rebuilded and my app is very heavy because rebuilds the previous product pages and everything else.
This happens due to the general rebuild on keyboard changes(i.e: showing and hiding Keyboard).
I recommend to have a heavy operation like loading data from backend should be held on initState in State with StatefullWidget.
Try to debug and understand why do you rebuild the tree. It seems to me, that you need to simplify a navigation flow.

how to send a data from page to another without change the screen?

i have a page named displayData . On it there is a button if i click on it it shows DraggableScrollableSheet normally it has a button done when i click on it the icon of the page on the bakground (DisplayData) must change .i used a variable in a setState but it doesn't work it's true on the DraggableScrollableSheet but always false in display data
any help !
for this type of cases, there are several solutions, I recommend the use of providers, in this way with the use of 'extends ChangeNotifier' you will be able to detect the changes made without having to use the setState and you will be able to call from any class (or page ) the variables you need
you can use callback methods from on widget to another, if you have much complicated scenario then I suggest using state management libraries like Provider, RiverPod or Bloc.
also
Could you share your code here

How to simulate device back btn press/ AppBar back press? WillPopScope is not called upon Navigator.pop()

I was able to override the back button onPressed behavior using WillPopScope. Now, whenever I press the device back button or the default AppBar back button, the code inside onWillPop runs as expected.
The problem now is that I want to have my own custom buttons to navigate backward, but Navigator.of(context).pop() doesn't check the WillPopScope as if it doesn't exist.
Is there a way to simulate the system's back behavior or call it somehow?
I know I can just call the same function that is called by
onWillPop() manually upon pressing my custom back button, but I was eager to find a cleaner method, as in the future I could easily forget doing that and cause bugs.
In simple cases, when you need to intercept the Android back-button, you usually add WillPopScope to your widget tree. However, when developing stateful widgets that interact with the back button, it's more convenient to use the BackButtonInterceptor.
For more details check this link: https://pub.dev/packages/back_button_interceptor
While using StateFullWidget, It is more convenient to use BackButtonInterceptor.
Hope this will Helpful.
For more Details Please check this:
[1]: https://pub.dev/packages/back_button_interceptor
The key was to use Navigator.of(context).maybePop()instead. Or more specifically in my case, was to use Navigator.of(context, rootNavigator:true).maybePop() as I was using nested Navigation.

How to load data on a screen on the press of a button without routing and rebuilding whole screen

Pardon me if this is a naive question but I am trying to figure out a way in Flutter to load different data when the user clicks a button. The way I currently see is routing the user to a new screen everytime but I am sure there would be a better approach without loading the whole screen everytime
You should use a Stateful Widget
I would create a Future Builder / ListView Builder. Depending on how you want it to behave you could have the ListView items clear before regenerating the list of items to show.
Future Builder you could create a function to return certain widgets depending on the request you make with the function.