Skip to content

Commit

Permalink
[two_dimensional_scrollables] Fix TreeView bug when animation duratio…
Browse files Browse the repository at this point in the history
…n is zero (#7475)

Fixes [#153889 ](flutter/flutter#154292) an issue where nodes were being removed incorrectly when using `AnimationStyle.noAnimation `or the animation duration was zero seconds, which previously caused the erratic behavior due to hidden state updates. Similar to flutter/flutter#153890.
  • Loading branch information
Mairramer authored Sep 11, 2024
1 parent 393ce9b commit 91caa7a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/two_dimensional_scrollables/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.2

* Fixes a bug where the TreeView would not update correctly when the animation duration is zero.

## 0.3.1

* Adds generics to the callbacks and builders of TreeView.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,15 @@ class _TreeViewState<T> extends State<TreeView<T>>
if (widget.onNodeToggle != null) {
widget.onNodeToggle!(node);
}

// If animation is disabled or duration is zero, skip the animation
// and update the active nodes immediately. This ensures the tree
// is updated correctly when the node's children are no longer active.
if (widget.toggleAnimationStyle?.duration == Duration.zero) {
_unpackActiveNodes();
return;
}

final AnimationController controller =
_currentAnimationForParent[node]?.controller ??
AnimationController(
Expand Down
2 changes: 1 addition & 1 deletion packages/two_dimensional_scrollables/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: two_dimensional_scrollables
description: Widgets that scroll using the two dimensional scrolling foundation.
version: 0.3.1
version: 0.3.2
repository: https://github.com/flutter/packages/tree/main/packages/two_dimensional_scrollables
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+two_dimensional_scrollables%22+

Expand Down
96 changes: 96 additions & 0 deletions packages/two_dimensional_scrollables/test/tree_view/tree_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,102 @@ void main() {
expect(treeView.treeNodeBuilder, isA<TreeViewNodeBuilder<String>>());
expect(treeView.treeRowBuilder, isA<TreeViewRowBuilder<String>>());
});

testWidgets(
'TreeViewNode should expand/collapse correctly when the animation duration is set to zero.',
(WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/154292
final TreeViewController controller = TreeViewController();
final List<TreeViewNode<String>> tree = <TreeViewNode<String>>[
TreeViewNode<String>('First'),
TreeViewNode<String>(
'Second',
children: <TreeViewNode<String>>[
TreeViewNode<String>(
'alpha',
children: <TreeViewNode<String>>[
TreeViewNode<String>('uno'),
TreeViewNode<String>('dos'),
TreeViewNode<String>('tres'),
],
),
TreeViewNode<String>('beta'),
TreeViewNode<String>('kappa'),
],
),
TreeViewNode<String>(
'Third',
expanded: true,
children: <TreeViewNode<String>>[
TreeViewNode<String>('gamma'),
TreeViewNode<String>('delta'),
TreeViewNode<String>('epsilon'),
],
),
TreeViewNode<String>('Fourth'),
];

await tester.pumpWidget(MaterialApp(
home: TreeView<String>(
tree: tree,
controller: controller,
toggleAnimationStyle: AnimationStyle(
curve: Curves.easeInOut,
duration: Duration.zero,
),
treeNodeBuilder: (
BuildContext context,
TreeViewNode<Object?> node,
AnimationStyle animationStyle,
) {
final Widget child = GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () => controller.toggleNode(node),
child: TreeView.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);
});
});

group('TreeViewport', () {
Expand Down

0 comments on commit 91caa7a

Please sign in to comment.