From 452099ab69c1c9f945aaeb876fe76c0a171e0b34 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Mon, 17 Mar 2025 16:15:36 -0700 Subject: [PATCH] `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]. [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 --- packages/flutter/lib/src/widgets/overlay.dart | 11 +---- .../test/scheduler/scheduler_test.dart | 23 +++++++--- .../widgets/overlay_layout_builder_test.dart | 45 +++++++++++++++++++ 3 files changed, 63 insertions(+), 16 deletions(-) diff --git a/packages/flutter/lib/src/widgets/overlay.dart b/packages/flutter/lib/src/widgets/overlay.dart index c75b22032c..6f8ae86385 100644 --- a/packages/flutter/lib/src/widgets/overlay.dart +++ b/packages/flutter/lib/src/widgets/overlay.dart @@ -2613,9 +2613,6 @@ class _OverlayChildLayoutBuilder extends AbstractLayoutBuilder 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, diff --git a/packages/flutter/test/scheduler/scheduler_test.dart b/packages/flutter/test/scheduler/scheduler_test.dart index 2244612cce..60febe3837 100644 --- a/packages/flutter/test/scheduler/scheduler_test.dart +++ b/packages/flutter/test/scheduler/scheduler_test.dart @@ -38,6 +38,20 @@ class TestSchedulerBinding extends BindingBase with SchedulerBinding, ServicesBi List> getEventsDispatched(String eventKind) { return eventsDispatched.putIfAbsent(eventKind, () => >[]); } + + 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); diff --git a/packages/flutter/test/widgets/overlay_layout_builder_test.dart b/packages/flutter/test/widgets/overlay_layout_builder_test.dart index 90a823f787..892b8d1645 100644 --- a/packages/flutter/test/widgets/overlay_layout_builder_test.dart +++ b/packages/flutter/test/widgets/overlay_layout_builder_test.dart @@ -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( + 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(