From f513a69838081acd3190c4d7cb3e7757b23b8fc6 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Thu, 12 Sep 2024 18:24:11 -0700 Subject: [PATCH] Mark `_LayoutBuilderElement` as always clean (#154694) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/flutter/flutter/issues/154060 The error message doesn't make sense to me since one can call `setState` during the idle phase, and I'm not sure what this is guarding against without the right error message. In the case of #154060 the layout builder was never laid out: ``` ├─child 1: _RenderLayoutBuilder#7c319 NEEDS-LAYOUT NEEDS-PAINT │ creator: LayoutBuilder ← _BodyBuilder ← MediaQuery ← │ LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← │ _ActionsScope ← Actions ← AnimatedBuilder ← DefaultTextStyle ← │ AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#1f6eb ink │ renderer] ← NotificationListener ← ⋯ │ parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.body │ constraints: MISSING │ size: MISSING ``` So https://github.com/flutter/flutter/pull/154681 doesn't really fix #154060 since the layout callback cannot be run without a set of valid constraints. Before the `BuildScope` change all `_inDirtyList` flags were unset after the `BuildOwner` finishes rebuilding the widget tree, so `LayoutBuilder._inDirtyLst` is always set to false after a frame even for layout builders that were never laid out. With the `BuildScope` change, `LayoutBuilder` has its own `BuildScope` which is only flushed after LayoutBuilder gets its constraints. --- .../flutter/lib/src/widgets/framework.dart | 13 ++++++------ .../lib/src/widgets/layout_builder.dart | 5 ++++- .../flutter/test/widgets/framework_test.dart | 12 +++++++++++ .../test/widgets/layout_builder_test.dart | 21 +++++++++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index cfbe6523c4..15394fd373 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -2911,15 +2911,14 @@ class BuildOwner { 'The dirty list for the current build scope is: ${buildScope._dirtyElements}', ); } - // When reactivating an inactivate Element, _scheduleBuildFor should only be - // called within _flushDirtyElements. if (!_debugBuilding && element._inDirtyList) { throw FlutterError.fromParts([ ErrorSummary('BuildOwner.scheduleBuildFor() called inappropriately.'), ErrorHint( - 'The BuildOwner.scheduleBuildFor() method should only be called while the ' - 'buildScope() method is actively rebuilding the widget tree.', + 'The BuildOwner.scheduleBuildFor() method called on an Element ' + 'that is already in the dirty list.', ), + element.describeElement('the dirty Element was'), ]); } return true; @@ -5145,8 +5144,10 @@ abstract class Element extends DiagnosticableTree implements BuildContext { /// Returns true if the element has been marked as needing rebuilding. /// /// The flag is true when the element is first created and after - /// [markNeedsBuild] has been called. The flag is reset to false in the - /// [performRebuild] implementation. + /// [markNeedsBuild] has been called. The flag is typically reset to false in + /// the [performRebuild] implementation, but certain elements (that of the + /// [LayoutBuilder] widget, for example) may choose to override [markNeedsBuild] + /// such that it does not set the [dirty] flag to `true` when called. bool get dirty => _dirty; bool _dirty = true; diff --git a/packages/flutter/lib/src/widgets/layout_builder.dart b/packages/flutter/lib/src/widgets/layout_builder.dart index 847e32bcb6..7052312eb6 100644 --- a/packages/flutter/lib/src/widgets/layout_builder.dart +++ b/packages/flutter/lib/src/widgets/layout_builder.dart @@ -158,7 +158,10 @@ class _LayoutBuilderElement extends RenderOb @override void markNeedsBuild() { - super.markNeedsBuild(); + // Calling super.markNeedsBuild is not needed. This Element does not need + // to performRebuild since this call already does what performRebuild does, + // So the element is clean as soon as this method returns and does not have + // to be added to the dirty list or marked as dirty. renderObject.markNeedsLayout(); _needsBuild = true; } diff --git a/packages/flutter/test/widgets/framework_test.dart b/packages/flutter/test/widgets/framework_test.dart index 4ba0a0619f..b1e7086f1d 100644 --- a/packages/flutter/test/widgets/framework_test.dart +++ b/packages/flutter/test/widgets/framework_test.dart @@ -1941,6 +1941,18 @@ The findRenderObject() method was called for the following element: expect(scopeElement.dirty, isFalse); expect(keyedWidget.dirty, isTrue); }); + + testWidgets('Calling scheduleBuildFor on an Element already in dirty list throws', (WidgetTester tester) async { + await tester.pumpWidget(const SizedBox()); + final Element element = tester.element(find.byType(SizedBox)); + element.markNeedsBuild(); + expect( + () => element.owner!.scheduleBuildFor(element), + throwsA(isFlutterError.having( + (FlutterError e) => e.message, 'message', contains('The BuildOwner.scheduleBuildFor() method called on an Element that is already in the dirty list.'), + )), + ); + }); } class _TestInheritedElement extends InheritedElement { diff --git a/packages/flutter/test/widgets/layout_builder_test.dart b/packages/flutter/test/widgets/layout_builder_test.dart index 51fbf778e0..4464a71f00 100644 --- a/packages/flutter/test/widgets/layout_builder_test.dart +++ b/packages/flutter/test/widgets/layout_builder_test.dart @@ -850,6 +850,27 @@ void main() { expect(paintCount, 3); expect(mostRecentOffset, const Offset(60, 60)); }); + + testWidgets('LayoutBuilder in a subtree that skips layout does not throw during the initial treewalk', (WidgetTester tester) async { + final OverlayEntry overlayEntry1 = OverlayEntry(maintainState: true, builder: (BuildContext context) => LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) => const Placeholder())); + // OverlayEntry2 obstructs OverlayEntry1 and forces it to skip layout. + final OverlayEntry overlayEntry2 = OverlayEntry(opaque: true, canSizeOverlay: true, builder: (BuildContext context) => Container()); + addTearDown(() => overlayEntry1..remove()..dispose()); + addTearDown(() => overlayEntry2..remove()..dispose()); + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + // The UnconstrainedBox makes sure the OverlayEntries are not relayout boundaries. + child: UnconstrainedBox(child: Overlay(initialEntries: [overlayEntry1, overlayEntry2])), + ), + ); + + WidgetsBinding.instance.buildOwner!.reassemble(WidgetsBinding.instance.rootElement!); + await tester.pump(); + WidgetsBinding.instance.buildOwner!.reassemble(WidgetsBinding.instance.rootElement!); + await tester.pump(); + expect(tester.takeException(), isNull); + }); } class _SmartLayoutBuilder extends ConstrainedLayoutBuilder {