I see there is a variable in test environment of Flutter called this:
tester.allWidgets
It can return all widgets within the view port at the moment.
Is there anyway I can do the same with Flutter code, not inside a unit test?
The specific requirement I have is to locate all scrollable widgets within the current view port. Wondering if there’s an easy way to achieve that.
2
Answers
use: https://pub.dev/packages/visibility_detector
VisibilityDetector
will detect child’s visibility. wrap the children withVisibilityDetector
, and use a list to containing all show widget.you will get list of
here’s a example:
You can try element tree traversal.
You locate widgets within the element tree that are scrollable and are within the current viewport by checking their position and size constraints.
Here is a sample of how you would do it:
We do a
renderBox.hasSize
to check if the RenderBox has been laid out by the framework and has a valid size.We do a
if (size.height > 0 && size.width > 0)
to check if the RenderBox has a positive width and height as zero height or width means that the widget is not visible or not rendered within the current viewport.