check if a widget is on screen - flutter

Is there a way to detect if a widget is on screen/seen anywhere on the app. For example in a TabBar or a PageView.
I already know that I could use the widget's build method to detect this, but this results in some really weird behavior where sometimes the widget has already been built and when the user navigates to the screen nothing happens.
So is there any way to do this with an entire app?

Maybe you can try using widget key like a Global Key
final key = GlobalKey();
and they passes it to the widget you want to know the state

Related

Should state practically be at the root of the tree (in most cases) in Flutter?

I'm utterly confused regarding the question of state management in Flutter.
So far I have read that the states of widgets have to be nested as deep in the tree as possible (or rather as close to the affected widget as possible) for better performance.
But what if such a nested state widget (InheritedWidget for example) has another state widget somewhere above it? Does the state widget at the lower level not loose it's state when the state widget higher in the tree requests rebuild for all of its descendants?
With this line of thought have I came to the conclusion, that state widgets just have to be at the root in the end, but I guess I'm wrong somehow.
The first part of your question is correct -
If a widget's state changes, this might require all its children to redraw.
But this is precisely why it is important to nest state as deep down in the widget tree as possible!
Assume the contrary, that all state information is stored at the root of the widget tree, at the very top.
Now if any information changes, no matter how small, it will lead to a complete traversal of the widget tree, rebuilding everything in the worst case.
And aside from the tree traversal, your application will also become very memory intensive. If all state is stored at the root, flutter can never tell when it is okay to release some information from memory. If the user leaves some views and the views are dismissed from memory, the information for them will still be stored at the top. And the only way to check wether that information is still needed would be to once again check the whole tree - very expensive!
All of this can be mitigated by putting your state as close as possible to the widget that will consume it. Because then
If the state changes, only a small subtree of the whole widget tree has to be traversed - This is fast.
If a widget is dismissed, flutter can also release all of the state information that has been stored for it. This frees memory.
yes ! every state widget has its own state and they are all independent. if the state of widget X is updated, only widget X will be updated
let suppose that you have an application that sows a family tree. in widget A you get the gradfather from an API, when you click on it you will be redirected to widget B where you can find his childrens, when you click on one of his childrens you go to widget C which shows the childrens of the selected father in widget B, now let's supposse that you want to add one children to this father.
you call the add-children endpoint. the problem here is that widget A will not be updated.
one solution to this, and to understand the state tree logic, is to pass a functionthat updates widget A from widget A to widget B and pass it from widget B to widget C and call it when an update happens either on widget B or C or even on A so widget A gets updated and you got the updated family tree in widget A
So basically flutter have it's own state management that is called setState(() {}) itu will update the state of the screen where setState is called if i have a button class widget in it's own file if i press the button i want to change the button name to something else so the setState will update the state or variables in the button class/widget.
Now how if the button wants to update a state/variables in the different class but in same screen? Since setState only update it's own class, so you to give the button onTap property with function constructer like this
final Function onButtonTap;
then put it on onTap like
onTap:() {
widget.onButtonTap();
}
Then in the screen where you want to update the state just call onButtonTap then use setState there

Does draggable stateful widget works?

I'm trying to make my first app and I have a problem:
I made a gridview of longpress draggable widget who can be deleted when I move them on the corresponding drag target.
Everything works well when I create these widget as statelesswidget.
The problem is that when I try to put these widget in Stateful widget it doesn't work well..
The choosen widget isn't removed correctly
Does someone already had this problem?
(Sorry for my Bad english)
Thanks

Can I store a widget from widget and later use it in another page without rebuilding it from scratch? - Flutter

I have a Stateful widget called Widget1 shown in one page. When I open my app the widget1 gets build from scratch and shown in the page. Let's assume the widget1 is quite heavy with lot's of other widget's inside it.
Now I want to display the same widget in another page but I don't want to build the same widget1 from scratch like I did in the first page. I want to reuse the widget which was built in the Previous page to be shown in this page.
Is that possible in flutter? I know flutter must build a widget inorder to make changes in Ui. But is this really possible? that is , to store a widget and use it, whenever we want without having to rebuild it, again from scratch?.

I have problems with flutter state management [duplicate]

This question already has answers here:
How to set state from another widget?
(2 answers)
Closed 2 years ago.
send help.
I'm a newbie flutter developer who's trying to build a simple calculator, but I can't figure out how to affect a widget's data from another widget (say a button). I'm trying to change the state of one widget from a very separate widget using buttons. How do I do this?
First you have to know the difference between StatelessWidget and StatefulWidget.
StatelessWidget : are those in which you want to make a UI that does not need to be dynamically changed whenever you update any value bound to it. For example, if you want to make a button whose title doesn't need to change dynamically, then you can create a separate widget for a button as a Stateless widget.
StatefulWidget : are just the reverse of Stateless widgets. This means when you want to make something that you want to change dynamically according to how a user interacts with it, then you can use the Stateful widget.
For example, if you want to change the background color of the app on click of a button, you can make use of Stateful widget in this case.
So if the state of the widget changes you have to call setState to trigger a rebuild of the view and see immediately the changes implied by the new state.
setState(() {
value = newValue ;
});
To know the topic in more detail, you can check this.

How to check visibility of a Flutter widget even when covered by another

I'm trying to find a way to check the visibility of a Flutter widget when it's either off screen or when it's obscured by another, for example Drawer, Dialog or BottomSheet.
visibility_detector helps with checking whether it's on the screen or not but does not work with the second use case (known limitation).
Is there a lower lever api that I can use to check whether a widget is actually visible to the user?
My use case: I'm adding a widget to the Overlay when something external happens (similar to Tooltip but not on long press). If the user opens for example the Drawer, the widget will appear on top. I want to detect that the child is not visible and delay or cancel the action.
Do I understand your problem?
You have a widget you want to always be on top?
You want the user to interact with that widget first before doing other things?
Use design patterns to make coding easier, your users will thank you.
You can show a Dialog on-top of other widgets with the showGeneralDialog or showDialog functions. Because Dialogs are a design-pattern used in many apps, users will already know how to use them.
Define app behavior with explicit state.
It is too hard to derive app behavior from rendered UI, not all devices are the same size and shape. This means you should try to write a variable that describes your situation and then write the code you need to make that variable's value correct. It sounds like you want a variable like bool overlayIsShowing.