From 324fdb6b3c4dc9b0cf314fa4937b30b8954738fc Mon Sep 17 00:00:00 2001 From: Yegor Date: Fri, 8 May 2020 12:27:59 -0700 Subject: [PATCH] LayoutBuilder: skip calling builder when constraints are the same (#55414) Avoid calling `builder` in `ConstrainedLayoutBuilder` when layout constraints are the same. [Design doc](flutter.dev/go/layout-builder-optimization). ## Related Issues Fixes https://github.com/flutter/flutter/issues/6469 --- .../flutter_gallery/lib/gallery/backdrop.dart | 8 ++ .../lib/src/widgets/layout_builder.dart | 67 +++++++++-- .../src/widgets/sliver_layout_builder.dart | 3 +- .../test/widgets/layout_builder_test.dart | 107 ++++++++++++++++++ 4 files changed, 177 insertions(+), 8 deletions(-) diff --git a/dev/integration_tests/flutter_gallery/lib/gallery/backdrop.dart b/dev/integration_tests/flutter_gallery/lib/gallery/backdrop.dart index 3f077c5ea0..527657c45e 100644 --- a/dev/integration_tests/flutter_gallery/lib/gallery/backdrop.dart +++ b/dev/integration_tests/flutter_gallery/lib/gallery/backdrop.dart @@ -215,6 +215,14 @@ class _BackdropState extends State with SingleTickerProviderStateMixin value: 1.0, vsync: this, ); + _controller.addStatusListener((AnimationStatus status) { + setState(() { + // This is intentionally left empty. The state change itself takes + // place inside the AnimationController, so there's nothing to update. + // All we want is for the widget to rebuild and read the new animation + // state from the AnimationController. + }); + }); _frontOpacity = _controller.drive(_frontOpacityTween); } diff --git a/packages/flutter/lib/src/widgets/layout_builder.dart b/packages/flutter/lib/src/widgets/layout_builder.dart index f7be789b20..b3ef06fd97 100644 --- a/packages/flutter/lib/src/widgets/layout_builder.dart +++ b/packages/flutter/lib/src/widgets/layout_builder.dart @@ -17,6 +17,18 @@ typedef LayoutWidgetBuilder = Widget Function(BuildContext context, BoxConstrain /// function at layout time and provides the constraints that this widget should /// adhere to. This is useful when the parent constrains the child's size and layout, /// and doesn't depend on the child's intrinsic size. +/// +/// {@template flutter.widgets.layoutBuilder.builderFunctionInvocation} +/// The [builder] function is called in the following situations: +/// +/// * The first time the widget is laid out. +/// * When the parent widget passes different layout constraints. +/// * When the parent widget updates this widget. +/// * When the depedencies that the [builder] function subscribes to change. +/// +/// The [builder] function is _not_ called during layout if the parent passes +/// the same constraints repeatedly. +/// {@endtemplate} abstract class ConstrainedLayoutBuilder extends RenderObjectWidget { /// Creates a widget that defers its building until layout. /// @@ -74,15 +86,22 @@ class _LayoutBuilderElement extends RenderOb assert(widget != newWidget); super.update(newWidget); assert(widget == newWidget); + renderObject.updateCallback(_layout); - renderObject.markNeedsLayout(); + // Force the callback to be called, even if the layout constraints are the + // same, because the logic in the callback might have changed. + renderObject.markNeedsBuild(); } @override void performRebuild() { // This gets called if markNeedsBuild() is called on us. // That might happen if, e.g., our builder uses Inherited widgets. - renderObject.markNeedsLayout(); + + // Force the callback to be called, even if the layout constraints are the + // same. This is because that callback may depend on the updated widget + // configuration, or an inherited widget. + renderObject.markNeedsBuild(); super.performRebuild(); // Calls widget.updateRenderObject (a no-op in this case). } @@ -168,10 +187,42 @@ mixin RenderConstrainedLayoutBuilder [0, 0, 0]); }); + + testWidgets('LayoutBuilder does not call builder when layout happens but layout constraints do not change', (WidgetTester tester) async { + int builderInvocationCount = 0; + + Future pumpTestWidget(Size size) async { + await tester.pumpWidget( + // Center is used to give the SizedBox the power to determine constraints for LayoutBuilder + Center( + child: SizedBox.fromSize( + size: size, + child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) { + builderInvocationCount += 1; + return _LayoutSpy(); + }), + ), + ), + ); + } + + await pumpTestWidget(const Size(10, 10)); + + final _RenderLayoutSpy spy = tester.renderObject(find.byType(_LayoutSpy)); + + // The child is laid out once the first time. + expect(spy.performLayoutCount, 1); + expect(spy.performResizeCount, 1); + + // The initial `pumpWidget` will trigger `performRebuild`, asking for + // builder invocation. + expect(builderInvocationCount, 1); + + // Invalidate the layout without chaning the constraints. + tester.renderObject(find.byType(LayoutBuilder)).markNeedsLayout(); + + // The second pump will not go through the `performRebuild` or `update`, and + // only judge the need for builder invocation based on constraints, which + // didn't change, so we don't expect any counters to go up. + await tester.pump(); + expect(builderInvocationCount, 1); + expect(spy.performLayoutCount, 1); + expect(spy.performResizeCount, 1); + + // Cause the `update` to be called (but not `performRebuild`), triggering + // builder invocation. + await pumpTestWidget(const Size(10, 10)); + expect(builderInvocationCount, 2); + + // The spy does not invalidate its layout on widget update, so no + // layout-related methods should be called. + expect(spy.performLayoutCount, 1); + expect(spy.performResizeCount, 1); + + // Have the child request layout and verify that the child gets laid out + // despite layout constraints remaining constant. + spy.markNeedsLayout(); + await tester.pump(); + + // Builder is not invoked. This was a layout-only pump with the same parent + // constraints. + expect(builderInvocationCount, 2); + + // Expect performLayout to be called. + expect(spy.performLayoutCount, 2); + + // performResize should not be called because the spy sets sizedByParent, + // and the constraints did not change. + expect(spy.performResizeCount, 1); + + // Change the parent size, triggering constraint change. + await pumpTestWidget(const Size(20, 20)); + + // We should see everything invoked once. + expect(builderInvocationCount, 3); + expect(spy.performLayoutCount, 3); + expect(spy.performResizeCount, 2); + }); +} + +class _LayoutSpy extends LeafRenderObjectWidget { + @override + LeafRenderObjectElement createElement() => _LayoutSpyElement(this); + + @override + RenderObject createRenderObject(BuildContext context) => _RenderLayoutSpy(); +} + +class _LayoutSpyElement extends LeafRenderObjectElement { + _LayoutSpyElement(LeafRenderObjectWidget widget) : super(widget); +} + +class _RenderLayoutSpy extends RenderBox { + int performLayoutCount = 0; + int performResizeCount = 0; + + @override + bool get sizedByParent => true; + + @override + void performResize() { + performResizeCount += 1; + size = constraints.biggest; + } + + @override + void performLayout() { + performLayoutCount += 1; + } }