From dc331d393be535b7178d1cc838151fd3f9fad212 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Tue, 22 Mar 2022 13:15:12 -0700 Subject: [PATCH] [RenderAnimatedSize] Resume interrupted resizing animation on attach (#100519) --- .../lib/src/rendering/animated_size.dart | 16 ++++ .../src/widgets/single_child_scroll_view.dart | 6 ++ .../test/widgets/animated_size_test.dart | 84 +++++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/packages/flutter/lib/src/rendering/animated_size.dart b/packages/flutter/lib/src/rendering/animated_size.dart index 10adca1872..9fb7e6f67c 100644 --- a/packages/flutter/lib/src/rendering/animated_size.dart +++ b/packages/flutter/lib/src/rendering/animated_size.dart @@ -174,6 +174,22 @@ class RenderAnimatedSize extends RenderAligningShiftedBox { _controller.resync(vsync); } + @override + void attach(PipelineOwner owner) { + super.attach(owner); + switch (state) { + case RenderAnimatedSizeState.start: + case RenderAnimatedSizeState.stable: + break; + case RenderAnimatedSizeState.changed: + case RenderAnimatedSizeState.unstable: + // Call markNeedsLayout in case the RenderObject isn't marked dirty + // already, to resume interrupted resizing animation. + markNeedsLayout(); + break; + } + } + @override void detach() { _controller.stop(); diff --git a/packages/flutter/lib/src/widgets/single_child_scroll_view.dart b/packages/flutter/lib/src/widgets/single_child_scroll_view.dart index a06a0c1276..898d616cb5 100644 --- a/packages/flutter/lib/src/widgets/single_child_scroll_view.dart +++ b/packages/flutter/lib/src/widgets/single_child_scroll_view.dart @@ -678,6 +678,12 @@ class _RenderSingleChildViewport extends RenderBox with RenderObjectWithChildMix ); } + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DiagnosticsProperty('offset', _paintOffset)); + } + @override Rect describeSemanticsClip(RenderObject child) { assert(axis != null); diff --git a/packages/flutter/test/widgets/animated_size_test.dart b/packages/flutter/test/widgets/animated_size_test.dart index ddbb444eda..d58d61f163 100644 --- a/packages/flutter/test/widgets/animated_size_test.dart +++ b/packages/flutter/test/widgets/animated_size_test.dart @@ -349,5 +349,89 @@ void main() { await pumpWidget(const Size(222, 222), const Duration(milliseconds: 10)); expect(tester.renderObject(find.byType(IntrinsicHeight)).size, const Size(222, 222)); }); + + testWidgets('re-attach with interrupted animation', (WidgetTester tester) async { + const Key key1 = ValueKey('key1'); + const Key key2 = ValueKey('key2'); + late StateSetter setState; + Size childSize = const Size.square(100); + final Widget animatedSize = Center( + key: GlobalKey(debugLabel: 'animated size'), + // This SizedBox creates a relayout boundary so _cleanRelayoutBoundary + // does not mark the descendant render objects below the relayout boundary + // dirty. + child: SizedBox.fromSize( + size: const Size.square(200), + child: Center( + child: AnimatedSize( + duration: const Duration(seconds: 1), + child: StatefulBuilder( + builder: (BuildContext context, StateSetter stateSetter) { + setState = stateSetter; + return SizedBox.fromSize(size: childSize); + }, + ), + ), + ), + ), + ); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Row( + children: [ + SizedBox( + key: key1, + height: 200, + child: animatedSize, + ), + const SizedBox( + key: key2, + height: 200, + ) + ], + ), + ) + ); + + setState(() { + childSize = const Size.square(150); + }); + // Kick off the resizing animation. + await tester.pump(); + + // Immediately reparent the AnimatedSize subtree to a different parent + // with the same incoming constraints. + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Row( + children: [ + const SizedBox( + key: key1, + height: 200, + ), + SizedBox( + key: key2, + height: 200, + child: animatedSize, + ) + ], + ), + ) + ); + + expect( + tester.renderObject(find.byType(AnimatedSize)).size, + const Size.square(100), + ); + await tester.pumpAndSettle(); + // The animatedSize widget animates to the right size. + expect( + tester.renderObject(find.byType(AnimatedSize)).size, + const Size.square(150), + ); + }); }); }