Skip to content

Commit

Permalink
[two_dimensional_scrollables] Fixes TreeViewNode collapsing not worki…
Browse files Browse the repository at this point in the history
…ng (#7474)

Fixes [154295](flutter/flutter#154295)
This change addresses bug [#154295](flutter/flutter#154295) by ensuring that nodes are handled correctly when closing their children.
  • Loading branch information
Mairramer authored Oct 17, 2024
1 parent 870114d commit 5582669
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/two_dimensional_scrollables/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## NEXT
## 0.3.3

* Fixes an issue where collapsing nodes in the TreeView didn't work correctly.

## 0.3.2

Expand Down
10 changes: 7 additions & 3 deletions packages/two_dimensional_scrollables/lib/src/tree_view/tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,12 @@ class _TreeViewState<T> extends State<TreeView<T>>
_currentAnimationForParent[node]!.controller.dispose();
_currentAnimationForParent.remove(node);
_updateActiveAnimations();
// If the node is collapsing, we need to unpack the active
// nodes to remove the ones that were removed from the tree.
// This is only necessary if the node is collapsing.
if (!node._expanded) {
_unpackActiveNodes();
}
case AnimationStatus.forward:
case AnimationStatus.reverse:
}
Expand Down Expand Up @@ -949,9 +955,7 @@ class _TreeViewState<T> extends State<TreeView<T>>
controller.forward();
case false:
// Collapsing
controller.reverse().then((_) {
_unpackActiveNodes();
});
controller.reverse();
}
});
}
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.2
version: 0.3.3
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
68 changes: 68 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 @@ -814,6 +814,74 @@ void main() {
expect(find.text('dos'), findsNothing);
expect(find.text('tres'), findsNothing);
});

testWidgets(
'TreeViewNode should close all child nodes when collapsed, once the animation is completed',
(WidgetTester tester) async {
final TreeViewController controller = TreeViewController();
final List<TreeViewNode<String>> tree = <TreeViewNode<String>>[
TreeViewNode<String>(
'First',
expanded: true,
children: <TreeViewNode<String>>[
TreeViewNode<String>(
'alpha',
expanded: true,
children: <TreeViewNode<String>>[
TreeViewNode<String>('uno'),
TreeViewNode<String>('dos'),
TreeViewNode<String>('tres'),
],
),
TreeViewNode<String>('beta'),
TreeViewNode<String>('kappa'),
],
),
];

await tester.pumpWidget(MaterialApp(
home: TreeView<String>(
tree: tree,
controller: controller,
toggleAnimationStyle: AnimationStyle(
curve: Curves.easeInOut,
duration: const Duration(milliseconds: 200),
),
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('alpha'), findsOneWidget);
expect(find.text('uno'), findsOneWidget);
expect(find.text('dos'), findsOneWidget);
expect(find.text('tres'), findsOneWidget);

// Using runAsync to handle collapse and animations properly.
await tester.runAsync(() async {
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 5582669

Please sign in to comment.