OverlayPortal.childLayoutBuilder should rebuild when OverlayPortal rebuilds. (#165331)

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

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

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

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
LongCatIsLooong
2025-03-17 16:15:36 -07:00
committed by GitHub
parent 6e74ee4833
commit 452099ab69
3 changed files with 63 additions and 16 deletions

View File

@@ -2613,9 +2613,6 @@ class _OverlayChildLayoutBuilder extends AbstractLayoutBuilder<OverlayChildLayou
RenderAbstractLayoutBuilderMixin<OverlayChildLayoutInfo, RenderBox> createRenderObject(
BuildContext context,
) => _RenderLayoutBuilder();
@override
bool updateShouldRebuild(_OverlayChildLayoutBuilder oldWidget) => oldWidget.builder != builder;
}
// A RenderBox that:
@@ -2709,14 +2706,10 @@ class _RenderLayoutBuilder extends RenderProxyBox
int? _callbackId;
@override
void performLayout() {
late OverlayChildLayoutInfo newLayoutInfo;
// The invokeLayoutCallback allows arbitrary access to the sizes of
// RenderBoxes the we know that have finished doing layout.
invokeLayoutCallback((_) => newLayoutInfo = _computeNewLayoutInfo());
if (newLayoutInfo != _layoutInfo) {
_layoutInfo = newLayoutInfo;
rebuildIfNecessary();
}
invokeLayoutCallback((_) => _layoutInfo = _computeNewLayoutInfo());
rebuildIfNecessary();
assert(_callbackId == null);
_callbackId ??= SchedulerBinding.instance.scheduleFrameCallback(
_frameCallback,

View File

@@ -38,6 +38,20 @@ class TestSchedulerBinding extends BindingBase with SchedulerBinding, ServicesBi
List<Map<String, dynamic>> getEventsDispatched(String eventKind) {
return eventsDispatched.putIfAbsent(eventKind, () => <Map<String, dynamic>>[]);
}
void tearDown() {
additionalHandleBeginFrame = null;
additionalHandleDrawFrame = null;
PlatformDispatcher.instance
..onBeginFrame = null
..onDrawFrame = null;
}
/// Ensures callbacks for [PlatformDispatcher.onBeginFrame] and
/// [PlatformDispatcher.onDrawFrame] are registered.
void registerFrameCallbacks() {
ensureFrameCallbacksRegistered();
}
}
class TestStrategy {
@@ -55,13 +69,7 @@ void main() {
scheduler = TestSchedulerBinding();
});
tearDown(() {
scheduler.additionalHandleBeginFrame = null;
scheduler.additionalHandleDrawFrame = null;
PlatformDispatcher.instance
..onBeginFrame = null
..onDrawFrame = null;
});
tearDown(() => scheduler.tearDown());
test('Tasks are executed in the right order', () {
final TestStrategy strategy = TestStrategy();
@@ -301,6 +309,7 @@ void main() {
warmUpBeginFrame();
scheduler.registerFrameCallbacks();
// Simulate an animation frame firing between warm-up begin frame and warm-up draw frame.
// Expect a timer that reschedules the frame.
expect(scheduler.hasScheduledFrame, isFalse);

View File

@@ -140,6 +140,51 @@ void main() {
expect(regularChildSize, childSize);
});
testWidgets('builder callback is called when OverlayPortal rebuilds', (
WidgetTester tester,
) async {
late StateSetter setState;
Color color = const Color(0x12345678);
late final OverlayEntry overlayEntry;
addTearDown(
() =>
overlayEntry
..remove()
..dispose(),
);
Widget builder(BuildContext _, OverlayChildLayoutInfo _) => ColoredBox(color: color);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Overlay(
initialEntries: <OverlayEntry>[
overlayEntry = OverlayEntry(
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setter) {
setState = setter;
return OverlayPortal.overlayChildLayoutBuilder(
controller: controller1,
overlayChildBuilder: builder,
child: const SizedBox(),
);
},
);
},
),
],
),
),
);
expect(find.byType(ColoredBox), paints..rect(color: color));
setState(() => color = const Color(0x87654321));
await tester.pump();
expect(find.byType(ColoredBox), paints..rect(color: color));
});
testWidgets('Positioned works in the builder', (WidgetTester tester) async {
late final OverlayEntry overlayEntry;
addTearDown(