From 2ea9edc1ad51c742d4f3c55610820a33456d2b62 Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Wed, 13 Sep 2023 18:16:14 -0500 Subject: [PATCH] Update KeepAlive.debugTypicalAncestorWidgetClass (#133498) --- .../flutter/lib/src/cupertino/dialog.dart | 3 +-- .../flutter/lib/src/widgets/framework.dart | 26 ++++++++++++++----- packages/flutter/lib/src/widgets/sliver.dart | 12 ++++++--- .../flutter/test/widgets/keep_alive_test.dart | 8 ++++++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/dialog.dart b/packages/flutter/lib/src/cupertino/dialog.dart index 2e4d27af75..4b969a2b31 100644 --- a/packages/flutter/lib/src/cupertino/dialog.dart +++ b/packages/flutter/lib/src/cupertino/dialog.dart @@ -1577,8 +1577,7 @@ class _ActionButtonParentDataWidget } @override - Type get debugTypicalAncestorWidgetClass => - _CupertinoDialogActionsRenderWidget; + Type get debugTypicalAncestorWidgetClass => _CupertinoDialogActionsRenderWidget; } // ParentData applied to individual action buttons that report whether or not diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index 34421ae2e5..ebe4e60e84 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -1592,15 +1592,19 @@ abstract class ParentDataWidget extends ProxyWidget { return renderObject.parentData is T; } - /// The [RenderObjectWidget] that is typically used to set up the [ParentData] - /// that [applyParentData] will write to. + /// Describes the [RenderObjectWidget] that is typically used to set up the + /// [ParentData] that [applyParentData] will write to. /// /// This is only used in error messages to tell users what widget typically - /// wraps this [ParentDataWidget]. + /// wraps this [ParentDataWidget] through + /// [debugTypicalAncestorWidgetDescription]. /// /// ## Implementations /// - /// The returned type should be a subclass of `RenderObjectWidget`. + /// The returned Type should describe a subclass of `RenderObjectWidget`. If + /// more than one Type is supported, use + /// [debugTypicalAncestorWidgetDescription], which typically inserts this + /// value but can be overridden to describe more than one Type. /// /// ```dart /// @override @@ -1612,6 +1616,16 @@ abstract class ParentDataWidget extends ProxyWidget { /// type is specialized), or specifying the upper bound (e.g. `Foo`). Type get debugTypicalAncestorWidgetClass; + /// Describes the [RenderObjectWidget] that is typically used to set up the + /// [ParentData] that [applyParentData] will write to. + /// + /// This is only used in error messages to tell users what widget typically + /// wraps this [ParentDataWidget]. + /// + /// Returns [debugTypicalAncestorWidgetClass] by default as a String. This can + /// be overridden to describe more than one Type of valid parent. + String get debugTypicalAncestorWidgetDescription => '$debugTypicalAncestorWidgetClass'; + Iterable _debugDescribeIncorrectParentDataType({ required ParentData? parentData, RenderObjectWidget? parentDataCreator, @@ -1632,7 +1646,7 @@ abstract class ParentDataWidget extends ProxyWidget { ), ErrorHint( 'Usually, this means that the $runtimeType widget has the wrong ancestor RenderObjectWidget. ' - 'Typically, $runtimeType widgets are placed directly inside $debugTypicalAncestorWidgetClass widgets.', + 'Typically, $runtimeType widgets are placed directly inside $debugTypicalAncestorWidgetDescription widgets.', ), if (parentDataCreator != null) ErrorHint( @@ -6300,7 +6314,7 @@ abstract class RenderObjectElement extends Element { ErrorSummary('Incorrect use of ParentDataWidget.'), ErrorDescription('The following ParentDataWidgets are providing parent data to the same RenderObject:'), for (final ParentDataElement ancestor in badAncestors) - ErrorDescription('- ${ancestor.widget} (typically placed directly inside a ${(ancestor.widget as ParentDataWidget).debugTypicalAncestorWidgetClass} widget)'), + ErrorDescription('- ${ancestor.widget} (typically placed directly inside a ${(ancestor.widget as ParentDataWidget).debugTypicalAncestorWidgetDescription} widget)'), ErrorDescription('However, a RenderObject can only receive parent data from at most one ParentDataWidget.'), ErrorHint('Usually, this indicates that at least one of the offending ParentDataWidgets listed above is not placed directly inside a compatible ancestor widget.'), ErrorDescription('The ownership chain for the RenderObject that received the parent data was:\n ${debugGetCreatorChain(10)}'), diff --git a/packages/flutter/lib/src/widgets/sliver.dart b/packages/flutter/lib/src/widgets/sliver.dart index 65ce9e48d7..f4bb14da73 100644 --- a/packages/flutter/lib/src/widgets/sliver.dart +++ b/packages/flutter/lib/src/widgets/sliver.dart @@ -1311,8 +1311,8 @@ class _SliverOffstageElement extends SingleChildRenderObjectElement { /// Mark a child as needing to stay alive even when it's in a lazy list that /// would otherwise remove it. /// -/// This widget is for use in [SliverWithKeepAliveWidget]s, such as -/// [SliverGrid] or [SliverList]. +/// This widget is for use in a [RenderAbstractViewport]s, such as +/// [Viewport] or [TwoDimensionalViewport]. /// /// This widget is rarely used directly. The [SliverChildBuilderDelegate] and /// [SliverChildListDelegate] delegates, used with [SliverList] and @@ -1322,6 +1322,9 @@ class _SliverOffstageElement extends SingleChildRenderObjectElement { /// each child, causing [KeepAlive] widgets to be automatically added and /// configured in response to [KeepAliveNotification]s. /// +/// The same `addAutomaticKeepAlives` feature is supported by the +/// [TwoDimensionalChildBuilderDelegate] and [TwoDimensionalChildListDelegate]. +/// /// Therefore, to keep a widget alive, it is more common to use those /// notifications than to directly deal with [KeepAlive] widgets. /// @@ -1365,7 +1368,10 @@ class KeepAlive extends ParentDataWidget { bool debugCanApplyOutOfTurn() => keepAlive; @override - Type get debugTypicalAncestorWidgetClass => SliverWithKeepAliveWidget; + Type get debugTypicalAncestorWidgetClass => throw FlutterError('Multiple Types are supported, use debugTypicalAncestorWidgetDescription.'); + + @override + String get debugTypicalAncestorWidgetDescription => 'SliverWithKeepAliveWidget or TwoDimensionalViewport'; @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { diff --git a/packages/flutter/test/widgets/keep_alive_test.dart b/packages/flutter/test/widgets/keep_alive_test.dart index 6bd324f990..c44843916d 100644 --- a/packages/flutter/test/widgets/keep_alive_test.dart +++ b/packages/flutter/test/widgets/keep_alive_test.dart @@ -47,6 +47,14 @@ List generateList(Widget child) { } void main() { + test('KeepAlive debugTypicalAncestorWidgetClass', () { + final KeepAlive keepAlive = KeepAlive(keepAlive: false, child: Container()); + expect( + keepAlive.debugTypicalAncestorWidgetDescription, + 'SliverWithKeepAliveWidget or TwoDimensionalViewport', + ); + }); + testWidgetsWithLeakTracking('KeepAlive with ListView with itemExtent', (WidgetTester tester) async { await tester.pumpWidget( Directionality(