Skip to content

Commit 5582669

Browse files
authored
[two_dimensional_scrollables] Fixes TreeViewNode collapsing not working (#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.
1 parent 870114d commit 5582669

File tree

4 files changed

+79
-5
lines changed

4 files changed

+79
-5
lines changed

packages/two_dimensional_scrollables/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
## NEXT
1+
## 0.3.3
2+
3+
* Fixes an issue where collapsing nodes in the TreeView didn't work correctly.
24

35
## 0.3.2
46

packages/two_dimensional_scrollables/lib/src/tree_view/tree.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,12 @@ class _TreeViewState<T> extends State<TreeView<T>>
911911
_currentAnimationForParent[node]!.controller.dispose();
912912
_currentAnimationForParent.remove(node);
913913
_updateActiveAnimations();
914+
// If the node is collapsing, we need to unpack the active
915+
// nodes to remove the ones that were removed from the tree.
916+
// This is only necessary if the node is collapsing.
917+
if (!node._expanded) {
918+
_unpackActiveNodes();
919+
}
914920
case AnimationStatus.forward:
915921
case AnimationStatus.reverse:
916922
}
@@ -949,9 +955,7 @@ class _TreeViewState<T> extends State<TreeView<T>>
949955
controller.forward();
950956
case false:
951957
// Collapsing
952-
controller.reverse().then((_) {
953-
_unpackActiveNodes();
954-
});
958+
controller.reverse();
955959
}
956960
});
957961
}

packages/two_dimensional_scrollables/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: two_dimensional_scrollables
22
description: Widgets that scroll using the two dimensional scrolling foundation.
3-
version: 0.3.2
3+
version: 0.3.3
44
repository: https://github.com/flutter/packages/tree/main/packages/two_dimensional_scrollables
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+two_dimensional_scrollables%22+
66

packages/two_dimensional_scrollables/test/tree_view/tree_test.dart

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,74 @@ void main() {
814814
expect(find.text('dos'), findsNothing);
815815
expect(find.text('tres'), findsNothing);
816816
});
817+
818+
testWidgets(
819+
'TreeViewNode should close all child nodes when collapsed, once the animation is completed',
820+
(WidgetTester tester) async {
821+
final TreeViewController controller = TreeViewController();
822+
final List<TreeViewNode<String>> tree = <TreeViewNode<String>>[
823+
TreeViewNode<String>(
824+
'First',
825+
expanded: true,
826+
children: <TreeViewNode<String>>[
827+
TreeViewNode<String>(
828+
'alpha',
829+
expanded: true,
830+
children: <TreeViewNode<String>>[
831+
TreeViewNode<String>('uno'),
832+
TreeViewNode<String>('dos'),
833+
TreeViewNode<String>('tres'),
834+
],
835+
),
836+
TreeViewNode<String>('beta'),
837+
TreeViewNode<String>('kappa'),
838+
],
839+
),
840+
];
841+
842+
await tester.pumpWidget(MaterialApp(
843+
home: TreeView<String>(
844+
tree: tree,
845+
controller: controller,
846+
toggleAnimationStyle: AnimationStyle(
847+
curve: Curves.easeInOut,
848+
duration: const Duration(milliseconds: 200),
849+
),
850+
treeNodeBuilder: (
851+
BuildContext context,
852+
TreeViewNode<Object?> node,
853+
AnimationStyle animationStyle,
854+
) {
855+
final Widget child = GestureDetector(
856+
behavior: HitTestBehavior.translucent,
857+
onTap: () => controller.toggleNode(node),
858+
child: TreeView.defaultTreeNodeBuilder(
859+
context,
860+
node,
861+
animationStyle,
862+
),
863+
);
864+
865+
return child;
866+
},
867+
),
868+
));
869+
870+
expect(find.text('alpha'), findsOneWidget);
871+
expect(find.text('uno'), findsOneWidget);
872+
expect(find.text('dos'), findsOneWidget);
873+
expect(find.text('tres'), findsOneWidget);
874+
875+
// Using runAsync to handle collapse and animations properly.
876+
await tester.runAsync(() async {
877+
await tester.tap(find.text('alpha'));
878+
await tester.pumpAndSettle();
879+
880+
expect(find.text('uno'), findsNothing);
881+
expect(find.text('dos'), findsNothing);
882+
expect(find.text('tres'), findsNothing);
883+
});
884+
});
817885
});
818886

819887
group('TreeViewport', () {

0 commit comments

Comments
 (0)