Skip to content
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

OverlayPortal.childLayoutBuilder should rebuild when OverlayPortal rebuilds. #165331

Conversation

LongCatIsLooong
Copy link
Contributor

The builder callback can capture free variables. So it must be re-evaluated every time OverlayPortal rebuilds.

Also defining the builder callback as a method in the State class seems to be a common pattern. So the oldWidget.builder != builder check may prevent necessary rebuilds when that's the case.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

…rebuilds.

The builder callback can capture free variables. So the it must be
re-evaluated every time `OverlayPortal` rebuilds.
@github-actions github-actions bot added the framework flutter/packages/flutter repository. See also f: labels. label Mar 17, 2025
}

@override
void ensureFrameCallbacksRegistered() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can create a different method to call the ensureFrameCallbacksRegistered

..dispose(),
);

Widget builder(BuildContext _, OverlayChildLayoutInfo _) => ColoredBox(color: color);
Copy link
Contributor

@chunhtai chunhtai Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this code is the correct usage of builder. In a widget build method, it will be developer's job to ensure the widget rebuild if it reference a state outside of the build method. Either turn that into changenotifier and use ChangeNotifierBuilder, or use inherited widget to do dependsOnInheritedWidget.

Copy link
Contributor Author

@LongCatIsLooong LongCatIsLooong Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's true. In a Builder / StatefulBuilder / LayoutBuilder the developer should be allowed to capture free variables in the callback. The framework should rebuild the entire widget tree if the root is dirty (unless it hits a cached widget) otherwise every builder callback will have to be closed functions because they may use variables defined in its parent widget's State. Otherwise the syntax will be way too verbose, and builder widgets will be a pain to use / debug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this makes sense consider that is true for other builder type widget. SGTM then

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

..dispose(),
);

Widget builder(BuildContext _, OverlayChildLayoutInfo _) => ColoredBox(color: color);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this makes sense consider that is true for other builder type widget. SGTM then

@@ -2615,7 +2615,7 @@ class _OverlayChildLayoutBuilder extends AbstractLayoutBuilder<OverlayChildLayou
) => _RenderLayoutBuilder();

@override
bool updateShouldRebuild(_OverlayChildLayoutBuilder oldWidget) => oldWidget.builder != builder;
bool updateShouldRebuild(_OverlayChildLayoutBuilder oldWidget) => true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as default implementation, I think we can remove this override

@LongCatIsLooong LongCatIsLooong added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 17, 2025
@auto-submit auto-submit bot added this pull request to the merge queue Mar 17, 2025
Merged via the queue into flutter:master with commit 452099a Mar 17, 2025
75 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Mar 17, 2025
@LongCatIsLooong LongCatIsLooong deleted the OverlayPortalChildLayoutBuilder-free-variable branch March 18, 2025 01:12
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants