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.
This commit is contained in:
@@ -872,6 +872,15 @@ class _TreeSliverState<T> extends State<TreeSliver<T>> 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,
|
||||
|
||||
@@ -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<TreeSliverNode<String>> tree = <TreeSliverNode<String>>[
|
||||
TreeSliverNode<String>('First'),
|
||||
TreeSliverNode<String>(
|
||||
'Second',
|
||||
children: <TreeSliverNode<String>>[
|
||||
TreeSliverNode<String>(
|
||||
'alpha',
|
||||
children: <TreeSliverNode<String>>[
|
||||
TreeSliverNode<String>('uno'),
|
||||
TreeSliverNode<String>('dos'),
|
||||
TreeSliverNode<String>('tres'),
|
||||
],
|
||||
),
|
||||
TreeSliverNode<String>('beta'),
|
||||
TreeSliverNode<String>('kappa'),
|
||||
],
|
||||
),
|
||||
TreeSliverNode<String>(
|
||||
'Third',
|
||||
expanded: true,
|
||||
children: <TreeSliverNode<String>>[
|
||||
TreeSliverNode<String>('gamma'),
|
||||
TreeSliverNode<String>('delta'),
|
||||
TreeSliverNode<String>('epsilon'),
|
||||
],
|
||||
),
|
||||
TreeSliverNode<String>('Fourth'),
|
||||
];
|
||||
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: CustomScrollView(
|
||||
slivers: <Widget>[
|
||||
TreeSliver<String>(
|
||||
tree: tree,
|
||||
controller: controller,
|
||||
toggleAnimationStyle: AnimationStyle.noAnimation,
|
||||
treeNodeBuilder: (
|
||||
BuildContext context,
|
||||
TreeSliverNode<Object?> 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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user