From 93d06ebd6cc638d8a215deb35dcd6fb6b9e76666 Mon Sep 17 00:00:00 2001 From: Mairramer <50643541+Mairramer@users.noreply.github.com> Date: Tue, 10 Sep 2024 19:24:54 -0300 Subject: [PATCH] fix unpack freezing app with animation duration zero (#153890) Fixes #153889 an issue where nodes were being removed incorrectly when using `AnimationStyle.noAnimation `or the animation duration was zero seconds, which previously caused the application to freeze due to hidden state updates. By skipping the animation and updating active nodes immediately in these cases, we avoid these issues and ensure smooth and accurate management of node states. --- .../flutter/lib/src/widgets/sliver_tree.dart | 9 ++ .../test/widgets/sliver_tree_test.dart | 95 +++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/packages/flutter/lib/src/widgets/sliver_tree.dart b/packages/flutter/lib/src/widgets/sliver_tree.dart index 3358461407..cc3b780530 100644 --- a/packages/flutter/lib/src/widgets/sliver_tree.dart +++ b/packages/flutter/lib/src/widgets/sliver_tree.dart @@ -872,6 +872,15 @@ class _TreeSliverState extends State> with TickerProviderStateM _currentAnimationForParent[node]!.animation.dispose(); } + // If animation is disabled or the duration is zero, we skip the animation + // and immediately update the active nodes. This prevents the app from freezing + // due to the tree being incorrectly updated when the animation duration is zero. + // This is because, in this case, the node's children are no longer active. + if (widget.toggleAnimationStyle == AnimationStyle.noAnimation || widget.toggleAnimationStyle?.duration == Duration.zero) { + _unpackActiveNodes(); + return; + } + final AnimationController controller = _currentAnimationForParent[node]?.controller ?? AnimationController( value: node._expanded ? 0.0 : 1.0, diff --git a/packages/flutter/test/widgets/sliver_tree_test.dart b/packages/flutter/test/widgets/sliver_tree_test.dart index a7a7a2581e..a002dee77f 100644 --- a/packages/flutter/test/widgets/sliver_tree_test.dart +++ b/packages/flutter/test/widgets/sliver_tree_test.dart @@ -788,4 +788,99 @@ void main() { expect(find.text('Child 2:1'), findsNothing); expect(find.text('Root 3'), findsOneWidget); }); + + testWidgets('TreeSliverNode should close all children when collapsed when animation is disabled', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/153889 + final TreeSliverController controller = TreeSliverController(); + final List> tree = >[ + TreeSliverNode('First'), + TreeSliverNode( + 'Second', + children: >[ + TreeSliverNode( + 'alpha', + children: >[ + TreeSliverNode('uno'), + TreeSliverNode('dos'), + TreeSliverNode('tres'), + ], + ), + TreeSliverNode('beta'), + TreeSliverNode('kappa'), + ], + ), + TreeSliverNode( + 'Third', + expanded: true, + children: >[ + TreeSliverNode('gamma'), + TreeSliverNode('delta'), + TreeSliverNode('epsilon'), + ], + ), + TreeSliverNode('Fourth'), + ]; + + await tester.pumpWidget(MaterialApp( + home: CustomScrollView( + slivers: [ + TreeSliver( + tree: tree, + controller: controller, + toggleAnimationStyle: AnimationStyle.noAnimation, + treeNodeBuilder: ( + BuildContext context, + TreeSliverNode node, + AnimationStyle animationStyle, + ) { + final Widget child = GestureDetector( + behavior: HitTestBehavior.translucent, + onTap: () => controller.toggleNode(node), + child: TreeSliver.defaultTreeNodeBuilder( + context, + node, + animationStyle, + ), + ); + + return child; + }, + ), + ], + ), + )); + + expect(find.text('First'), findsOneWidget); + expect(find.text('Second'), findsOneWidget); + expect(find.text('Third'), findsOneWidget); + expect(find.text('Fourth'), findsOneWidget); + expect(find.text('alpha'), findsNothing); + expect(find.text('beta'), findsNothing); + expect(find.text('kappa'), findsNothing); + expect(find.text('gamma'), findsOneWidget); + expect(find.text('delta'), findsOneWidget); + expect(find.text('epsilon'), findsOneWidget); + expect(find.text('uno'), findsNothing); + expect(find.text('dos'), findsNothing); + expect(find.text('tres'), findsNothing); + + await tester.tap(find.text('Second')); + await tester.pumpAndSettle(); + + expect(find.text('alpha'), findsOneWidget); + + await tester.tap(find.text('alpha')); + await tester.pumpAndSettle(); + + expect(find.text('uno'), findsOneWidget); + expect(find.text('dos'), findsOneWidget); + expect(find.text('tres'), findsOneWidget); + + await tester.tap(find.text('alpha')); + await tester.pumpAndSettle(); + + expect(find.text('uno'), findsNothing); + expect(find.text('dos'), findsNothing); + expect(find.text('tres'), findsNothing); + }); }