Fixes TreeSliver collapsing not working (#153438)

This change addresses bug #153418 by ensuring that nodes are handled correctly when closing their children.

Fixes #153418
This commit is contained in:
Mairramer
2024-10-17 15:12:18 -03:00
committed by GitHub
parent 117bb4788b
commit 6acc5915b4
2 changed files with 86 additions and 3 deletions

View File

@@ -862,6 +862,7 @@ class _TreeSliverState<T> extends State<TreeSliver<T>> with TickerProviderStateM
// No state to change.
return;
}
setState(() {
node._expanded = !node._expanded;
if (widget.onNodeToggle != null) {
@@ -895,6 +896,12 @@ class _TreeSliverState<T> extends State<TreeSliver<T>> with TickerProviderStateM
_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:
}
@@ -931,9 +938,7 @@ class _TreeSliverState<T> extends State<TreeSliver<T>> with TickerProviderStateM
controller.forward();
case false:
// Collapsing
controller.reverse().then((_) {
_unpackActiveNodes();
});
controller.reverse();
}
});
}

View File

@@ -883,4 +883,82 @@ void main() {
expect(find.text('dos'), findsNothing);
expect(find.text('tres'), findsNothing);
});
testWidgets('TreeSliverNode should close all children when collapsed when animation is completed', (WidgetTester tester) async {
final TreeSliverController controller = TreeSliverController();
final List<TreeSliverNode<String>> tree = <TreeSliverNode<String>>[
TreeSliverNode<String>(
'First',
expanded: true,
children: <TreeSliverNode<String>>[
TreeSliverNode<String>(
'alpha',
expanded: true,
children: <TreeSliverNode<String>>[
TreeSliverNode<String>('uno'),
TreeSliverNode<String>('dos'),
TreeSliverNode<String>('tres'),
],
),
TreeSliverNode<String>('beta'),
TreeSliverNode<String>('kappa'),
],
),
];
Widget buildTreeSliver(TreeSliverController controller) {
return MaterialApp(
home: Scaffold(
body: CustomScrollView(
shrinkWrap: true,
slivers: <Widget>[
TreeSliver<String>(
tree: tree,
controller: controller,
toggleAnimationStyle: AnimationStyle(
curve: Curves.easeInOut,
duration: const Duration(milliseconds: 200),
),
treeNodeBuilder: (
BuildContext context,
TreeSliverNode<Object?> node,
AnimationStyle animationStyle,
) {
final Widget child = GestureDetector(
key: ValueKey<String>(node.content! as String),
behavior: HitTestBehavior.translucent,
onTap: () => controller.toggleNode(node),
child: TreeSliver.defaultTreeNodeBuilder(
context,
node,
animationStyle,
),
);
return child;
},
),
],
),
),
);
}
await tester.pumpWidget(buildTreeSliver(controller));
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);
});
});
}