-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Update example code and docs for InteractiveViewer.builder #96019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update example code and docs for InteractiveViewer.builder #96019
Conversation
a2ef628 to
1e13abf
Compare
examples/api/lib/widgets/interactive_viewer/interactive_viewer.builder.0.dart
Outdated
Show resolved
Hide resolved
examples/api/lib/widgets/interactive_viewer/interactive_viewer.builder.0.dart
Outdated
Show resolved
Hide resolved
examples/api/lib/widgets/interactive_viewer/interactive_viewer.builder.0.dart
Outdated
Show resolved
Hide resolved
1e13abf to
f81ed79
Compare
|
Can you fix the analyzer issues please? Also, we have started to add tests for these samples. Can you please add one that covers your change? Thanks! |
|
@goderbauer I fixed the analyzer issues, thanks for pointing that out. I will add the unit tests as soon as I can. If you have any examples to point me towards it's greatly appreciated, no worries if not I can probably find some. This is my first PR that touches flutter! |
justinmc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for cleaning up this example! I should have updated it when I did Lazy Flutter Performance. This looks more useful and easier to understand, I'm on board. Just a few small nits you might want to consider.
LGTM 👍
examples/api/lib/widgets/interactive_viewer/interactive_viewer.builder.0.dart
Outdated
Show resolved
Hide resolved
examples/api/lib/widgets/interactive_viewer/interactive_viewer.builder.0.dart
Outdated
Show resolved
Hide resolved
examples/api/lib/widgets/interactive_viewer/interactive_viewer.builder.0.dart
Outdated
Show resolved
Hide resolved
examples/api/lib/widgets/interactive_viewer/interactive_viewer.builder.0.dart
Outdated
Show resolved
Hide resolved
examples/api/lib/widgets/interactive_viewer/interactive_viewer.builder.0.dart
Outdated
Show resolved
Hide resolved
justinmc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I forgot that these examples can be tested now. I'll find you an example.
|
Here's an example of testing an example: The example: https://github.com/flutter/flutter/blob/master/examples/api/lib/widgets/layout_builder/layout_builder.0.dart Maybe something like this would make a good test for this example:
|
ddff8a1 to
47418fe
Compare
|
Thanks for all the help! Hopefully this test covers enough. |
cdc48cf to
4facbc4
Compare
d680bcd to
a5b866e
Compare
|
@justinmc I noticed that this still lacks performance when zoomed out and many cells are on screen (this is why I increased the dimensions of each cell). However, the same # of cells being rendered via the standard child form has no performance issue. My understanding is that this is due to the cells being rebuilt each tick with I'm pretty new to Flutter and couldn't figure out a way around this. Using the same If you have any thoughts on this or time to help I'd greatly appreciate it. Hoping to be able to use this |
justinmc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
So sorry this took me forever to re-review, I need to clean out my Github notifications. Thanks for adding the test.
If you want to try to improve the performance of the rebuilding cells, I have 2 ideas. At a glance I think they are being rebuilt every time the InteractiveViewer transformation matrix changes, which is a ton, probably 60 x per second during a pan gesture.
- As a quick improvement, consider giving them a key that can uniquely identify them between builds. The widgets will still be rebuilt, but that might allow the underlying RenderObjects/Elements to be reused if I'm correct. Something like
ValueKey('$row,$col'). - You could try to cache the cell widgets. Any widgets that are being instantiated inside of InteractiveViewer's
buildermethod will be re-instantiated every frame that InteractiveViewer zooms or pans. Doing something fancier there so we only instantiate/dispose the cells when they enter/leave the screen would probably help a lot.
I'll wait to merge this in case you want to take a look at the performance, or that could also be another PR.
|
@gspencergoog Tagged you for secondary review since this uses your new example testing. |
gspencergoog
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| child: builder(context, row, col)), | ||
| ], | ||
| )); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a couple of commas and reformat for better formatting.
| child: builder(context, row, col)), | |
| ], | |
| )); | |
| child: builder(context, row, col), | |
| ), | |
| ], | |
| ), | |
| ); |
| alignment: Alignment.centerLeft, | ||
| child: Text('$row x $column'), | ||
| ), | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a comma between the "}" and the ")" on the next line and reformat. It'll cascade nicely then (I know it was like that before, but might was well fix it).
|
(triage) @AleLazar Can you please fix the two nits so we can submit this? Thanks! |
|
Superseded by #98623. |

Fixes #96005
The old example code laid out every single cell and did visibility checks on each of them. This resulted in poor performance and is not a viable pattern to copy.
This takes inspiration from the official youtube video on this builder form to turn this example into an infinite grid. This also adds this video to the documentation because it's really low in the SEO yet very valuable to understand the builder constructor.
While performance can still be hit (especially in debug mode) it degrades with # of cells on screen, rather than # of cells in total.
Pre-launch Checklist
///).