### TreeIndentation and IndentGuide Examples (Dart) Source: https://context7.com/alyssonpp/flutter_tree_view2/llms.txt Demonstrates how to use TreeIndentation with different IndentGuide styles (connecting lines, scoping lines, blank) for visual tree node structuring. It also shows how to provide a default indent guide to an entire subtree using DefaultIndentGuide. ```dart import 'package:flutter/material.dart'; import 'package:flutter_fancy_tree_view2/flutter_fancy_tree_view2.dart'; // Basic indentation with connecting lines Widget buildNodeTile(TreeEntry entry) { return TreeIndentation( entry: entry, guide: const IndentGuide.connectingLines( indent: 48, color: Colors.grey, thickness: 1.0, origin: 0.5, roundCorners: true, connectBranches: true, ), child: Row( children: [ FolderButton( isOpen: entry.hasChildren ? entry.isExpanded : null, onPressed: entry.hasChildren ? () => toggleNode(entry.node) : null, ), Expanded(child: Text(entry.node.title)), ], ), ); } // Alternative: scoping lines style Widget buildScopingNode(TreeEntry entry) { return TreeIndentation( entry: entry, guide: const IndentGuide.scopingLines( indent: 40, color: Colors.blue, thickness: 2.0, ), child: Text(entry.node.title), ); } // Blank indentation (no lines) Widget buildBlankIndent(TreeEntry entry) { return TreeIndentation( entry: entry, guide: const IndentGuide(indent: 40), child: Text(entry.node.title), ); } // Provide default indent guide to entire subtree Widget buildTreeWithDefaultGuide() { return DefaultIndentGuide( guide: const IndentGuide.connectingLines(indent: 32), child: TreeView( treeController: treeController, nodeBuilder: (context, entry) { // TreeIndentation will use DefaultIndentGuide.of(context) return TreeIndentation( entry: entry, child: Text(entry.node.title), ); }, ), ); } ``` -------------------------------- ### Render Tree Node with Indentation and Guides in Flutter Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/example/example.md This Flutter code snippet demonstrates how to render a tree node with indentation and optional connecting lines as an indent guide. It utilizes the `TreeIndentation` widget, which takes an `entry` and a `guide`. The `guide` parameter accepts an `IndentGuide` widget, such as `IndentGuide.connectingLines`, to visually represent the indentation hierarchy. The child of `TreeIndentation` is a `Padding` widget containing a `Row` with a `FolderButton` to indicate expansion state and a `Text` widget to display the node's title. ```dart child: TreeIndentation( entry: entry, // Provide an indent guide if desired. Indent guides can be used to // add decorations to the indentation of tree nodes. // This could also be provided through a DefaultTreeIndentGuide // inherited widget placed above the tree view. guide: const IndentGuide.connectingLines(indent: 48), // The widget to render next to the indentation. TreeIndentation // respects the text direction of `Directionality.maybeOf(context)` // and defaults to left-to-right. child: Padding( padding: const EdgeInsets.fromLTRB(4, 8, 8, 8), child: Row( children: [ // Add a widget to indicate the expansion state of this node. // See also: ExpandIcon. FolderButton( isOpen: entry.hasChildren ? entry.isExpanded : null, onPressed: entry.hasChildren ? onTap : null, ), Text(entry.node.title), ], ), ), ), ); } } ``` -------------------------------- ### Flutter Tree View Basic Implementation Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/example/example.md This Dart code snippet demonstrates the basic setup for a tree view using the flutter_tree_view2 package. It includes defining a data model, initializing the TreeController with root nodes and a children provider, and building the TreeView widget with a custom node builder. ```dart import 'package:flutter/material.dart'; import 'package:flutter_fancy_tree_view2/flutter_fancy_tree_view2.dart'; void main() => runApp(const MaterialApp(home: Scaffold(body: MyTreeView()))); // Create a class to hold your hierarchical data (optional, could be a Map or // any other data structure that's capable of representing hierarchical data). class MyNode { const MyNode({ required this.title, this.children = const [], }); final String title; final List children; } class MyTreeView extends StatefulWidget { const MyTreeView({super.key}); @override State createState() => _MyTreeViewState(); } class _MyTreeViewState extends State { // In this example a static nested tree is used, but your hierarchical data // can be composed and stored in many different ways. static const List roots = [ MyNode( title: 'Root 1', children: [ MyNode( title: 'Node 1.1', children: [ MyNode(title: 'Node 1.1.1'), MyNode(title: 'Node 1.1.2'), ], ), MyNode(title: 'Node 1.2'), ], ), MyNode( title: 'Root 2', children: [ MyNode( title: 'Node 2.1', children: [ MyNode(title: 'Node 2.1.1'), ], ), MyNode(title: 'Node 2.2') ], ), ]; // This controller is responsible for both providing your hierarchical data // to tree views and also manipulate the states of your tree nodes. late final TreeController treeController; @override void initState() { super.initState(); treeController = TreeController( // Provide the root nodes that will be used as a starting point when // traversing your hierarchical data. roots: roots, // Provide a callback for the controller to get the children of a // given node when traversing your hierarchical data. Avoid doing // heavy computations in this method, it should behave like a getter. childrenProvider: (MyNode node) => node.children, ); } @override void dispose() { // Remember to dispose your tree controller to release resources. treeController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { // This package provides some different tree views to customize how // your hierarchical data is incorporated into your app. In this example, // a TreeView is used which has no custom behaviors, if you wanted your // tree nodes to animate in and out when the parent node is expanded // and collapsed, the AnimatedTreeView could be used instead. // // The tree view widgets also have a Sliver variant to make it easy // to incorporate your hierarchical data in sophisticated scrolling // experiences. return TreeView( // This controller is used by tree views to build a flat representation // of a tree structure so it can be lazy rendered by a SliverList. // It is also used to store and manipulate the different states of the // tree nodes. treeController: treeController, // Provide a widget builder callback to map your tree nodes into widgets. nodeBuilder: (BuildContext context, TreeEntry entry) { // Provide a widget to display your tree nodes in the tree view. // // Can be any widget, just make sure to include a [TreeIndentation] // within its widget subtree to properly indent your tree nodes. return MyTreeTile( // Add a key to your tiles to avoid syncing descendant animations. key: ValueKey(entry.node), // Your tree nodes are wrapped in TreeEntry instances when traversing // the tree, these objects hold important details about its node // relative to the tree, like: expansion state, level, parent, etc. // // TreeEntrys are short lived, each time TreeController.rebuild is // called, a new TreeEntry is created for each node so its properties // are always up to date. entry: entry, // Add a callback to toggle the expansion state of this node. onTap: () => treeController.toggleExpansion(entry.node), ); }, ); } } // Create a widget to display the data held by your tree nodes. class MyTreeTile extends StatelessWidget { const MyTreeTile({ super.key, required this.entry, required this.onTap, }); final TreeEntry entry; final VoidCallback onTap; @override Widget build(BuildContext context) { return InkWell( onTap: onTap, // Wrap your content in a TreeIndentation widget which will properly child: Padding( padding: EdgeInsets.only(left: entry.level * 20.0), // Indentation based on level child: Text(entry.node.title), ), ); } } ``` -------------------------------- ### FolderButton Widget Example (Dart) Source: https://context7.com/alyssonpp/flutter_tree_view2/llms.txt Illustrates the usage of the FolderButton widget for creating interactive folder icons in a tree view. It supports automatic animation between open, closed, and leaf states, and allows for custom icons and animation settings. ```dart import 'package:flutter/material.dart'; import 'package:flutter_fancy_tree_view2/flutter_fancy_tree_view2.dart'; Widget buildFolderButton(TreeEntry entry, TreeController controller) { return FolderButton( // null = show file icon (leaf node), true = open folder, false = closed folder isOpen: entry.hasChildren ? entry.isExpanded : null, onPressed: entry.hasChildren ? () => controller.toggleExpansion(entry.node) : null, // Custom icons icon: const Icon(Icons.article), // Shown when isOpen is null openedIcon: const Icon(Icons.folder_open), // Shown when isOpen is true closedIcon: const Icon(Icons.folder), // Shown when isOpen is false // Animation settings duration: const Duration(milliseconds: 200), curve: Curves.easeInOut, transitionBuilder: (child, animation) { return RotationTransition(turns: animation, child: child); }, // IconButton properties iconSize: 24, color: Colors.blue, tooltip: 'Toggle folder', ); } ``` -------------------------------- ### Render Tree Views with TreeView and AnimatedTreeView in Dart Source: https://context7.com/alyssonpp/flutter_tree_view2/llms.txt Illustrates how to use `TreeView` and `AnimatedTreeView` widgets to display hierarchical data managed by a `TreeController`. `TreeView` offers basic rendering, while `AnimatedTreeView` adds smooth animations for expansion and collapse transitions. Customization options for animations and node building are included. ```dart import 'package:flutter/material.dart'; import 'package:flutter_fancy_tree_view2/flutter_fancy_tree_view2.dart'; class MyTreeView extends StatefulWidget { const MyTreeView({super.key}); @override State createState() => _MyTreeViewState(); } class _MyTreeViewState extends State { late final TreeController treeController; @override void initState() { super.initState(); treeController = TreeController( roots: myRootNodes, childrenProvider: (node) => node.children, ); } @override void dispose() { treeController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { // Basic TreeView without animations return TreeView( treeController: treeController, nodeBuilder: (BuildContext context, TreeEntry entry) { return ListTile( title: Text(entry.node.title), leading: Icon(entry.hasChildren ? Icons.folder : Icons.article), onTap: () => treeController.toggleExpansion(entry.node), ); }, ); } } // AnimatedTreeView with custom animation settings Widget buildAnimatedTree() { return AnimatedTreeView( treeController: treeController, duration: const Duration(milliseconds: 300), curve: Curves.easeInOut, maxNodesToShowWhenAnimating: 50, transitionBuilder: (context, child, animation) { return FadeTransition( opacity: animation, child: SizeTransition( sizeFactor: animation, child: child, ), ); }, nodeBuilder: (context, entry) { return MyNodeTile(entry: entry); }, ); } ``` -------------------------------- ### Manage Tree State with TreeController in Dart Source: https://context7.com/alyssonpp/flutter_tree_view2/llms.txt Demonstrates how to initialize and use `TreeController` to manage the state of a tree view. It covers defining node models, providing data, and performing expansion/collapse operations. This controller is essential for dynamic tree manipulation. ```dart import 'package:flutter_fancy_tree_view2/flutter_fancy_tree_view2.dart'; // Define your node model class MyNode { MyNode({required this.title, this.children = const []}); final String title; final List children; MyNode? parent; } // Create hierarchical data final List roots = [ MyNode( title: 'Root 1', children: [ MyNode(title: 'Child 1.1'), MyNode(title: 'Child 1.2'), ], ), MyNode(title: 'Root 2'), ]; // Create the controller final treeController = TreeController( roots: roots, childrenProvider: (MyNode node) => node.children, parentProvider: (MyNode node) => node.parent, // Required for drag-and-drop defaultExpansionState: false, // Nodes collapsed by default ); // Expansion operations treeController.expand(roots.first); // Expand single node treeController.collapse(roots.first); // Collapse single node treeController.toggleExpansion(roots.first); // Toggle expansion state treeController.expandAll(); // Expand all nodes treeController.collapseAll(); // Collapse all nodes treeController.expandCascading([roots.first]); // Expand node and all descendants treeController.collapseCascading([roots.first]); // Collapse node and all descendants treeController.expandAncestors(roots.first.children.first); // Expand all ancestors // Check expansion state bool isExpanded = treeController.getExpansionState(roots.first); bool allExpanded = treeController.isTreeExpanded; bool allCollapsed = treeController.isTreeCollapsed; // Update the tree view after data changes treeController.rebuild(); // Cleanup treeController.dispose(); ``` -------------------------------- ### Build Tree Node Widget with TreeEntry Context (Dart) Source: https://context7.com/alyssonpp/flutter_tree_view2/llms.txt Demonstrates how to build a custom widget for a tree node using the `TreeEntry` object. It accesses node data, level, index, expansion state, and sibling information to render the node with appropriate indentation and UI elements. This function assumes the existence of a `TreeController` and a `MyNode` data class. ```dart import 'package:flutter_fancy_tree_view2/flutter_fancy_tree_view2.dart'; Widget buildNodeWidget(TreeEntry entry) { // Access node data MyNode node = entry.node; // Tree structure information int level = entry.level; // Depth in tree (0 for roots) int index = entry.index; // Index in flat list bool hasChildren = entry.hasChildren; // Has child nodes bool isExpanded = entry.isExpanded; // Current expansion state bool hasNextSibling = entry.hasNextSibling; // Has sibling after it // Parent access (for traversing up the tree) TreeEntry? parent = entry.parent; // Skip indentation for root nodes bool skipIndent = entry.skipIndentAndPaint; // True if level <= 0 return Container( margin: EdgeInsets.only(left: level * 24.0), child: Row( children: [ if (hasChildren) IconButton( icon: Icon(isExpanded ? Icons.expand_more : Icons.chevron_right), onPressed: () => controller.toggleExpansion(node), ) else const SizedBox(width: 48), Text('${node.title} (Level $level, Index $index)'), if (!hasNextSibling) const Text(' [Last child]') ], ), ); } ``` -------------------------------- ### Implement Drag-and-Drop Tree View with Flutter Source: https://context7.com/alyssonpp/flutter_tree_view2/llms.txt This Dart code demonstrates how to set up a draggable tree view using TreeDraggable and TreeDragTarget. It includes a custom node model with parent references and a TreeController configured for drag-and-drop operations. The buildDraggableNode function defines the UI for draggable nodes and drop targets, handling node acceptance and visual feedback during drag operations. ```dart import 'package:flutter/material.dart'; import 'package:flutter_fancy_tree_view2/flutter_fancy_tree_view2.dart'; // Node model with parent reference (required for drag-and-drop) class DraggableNode { DraggableNode({required this.id, Iterable? children}) : _children = [] { if (children != null) { for (final child in children) { child._parent = this; _children.add(child); } } } final int id; final List _children; DraggableNode? _parent; List get children => _children; DraggableNode? get parent => _parent; int get index => _parent?._children.indexOf(this) ?? -1; void insertChild(int index, DraggableNode node) { node._parent?._children.remove(node); node._parent = this; _children.insert(index, node); } } // TreeController with parentProvider (required) final treeController = TreeController( roots: rootNodes, // Assuming rootNodes is defined elsewhere childrenProvider: (node) => node.children, parentProvider: (node) => node.parent, // Required for drag-and-drop! ); // Build draggable tree node Widget buildDraggableNode(TreeEntry entry) { return TreeDragTarget( node: entry.node, toggleExpansionOnHover: true, toggleExpansionDelay: const Duration(seconds: 1), onNodeAccepted: (TreeDragAndDropDetails details) { // Determine drop position based on where user dropped final oneThird = details.targetBounds.height / 3; final dropY = details.dropPosition.dy; DraggableNode? newParent; int newIndex = 0; if (dropY < oneThird) { // Drop above target - insert as previous sibling newParent = details.targetNode.parent; newIndex = details.targetNode.index; } else if (dropY < oneThird * 2) { // Drop inside target - insert as last child newParent = details.targetNode; newIndex = details.targetNode.children.length; treeController.setExpansionState(details.targetNode, true); } else { // Drop below target - insert as next sibling newParent = details.targetNode.parent; newIndex = details.targetNode.index + 1; } newParent?.insertChild(newIndex, details.draggedNode); treeController.rebuild(); }, builder: (context, TreeDragAndDropDetails? details) { // Highlight drop zone when dragging over BoxDecoration? decoration; if (details != null) { decoration = BoxDecoration( border: Border.all(color: Colors.blue, width: 2), ); } return TreeDraggable( node: entry.node, collapseOnDragStart: true, expandOnDragEnd: false, autoScrollSensitivity: 100.0, longPressDelay: const Duration(milliseconds: 500), // For touch devices feedback: Material( elevation: 4, child: Text('Dragging: Node ${entry.node.id}'), ), childWhenDragging: Opacity( opacity: 0.5, child: Text('Node ${entry.node.id}'), ), child: Container( decoration: decoration, child: TreeIndentation( entry: entry, child: Text('Node ${entry.node.id}'), ), ), ); }, ); } ``` -------------------------------- ### Configure TreeController with Parent Provider Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/README.md Configure the TreeController to use a parentProvider callback. This callback is essential for drag and drop operations and methods like expandAncestors, allowing the controller to access the parent of any given node. If not provided, a default callback returning null is used, which may lead to assertion errors in debug mode for dependent methods. ```dart final treeController = TreeController( ..., parentProvider: (MyTreeNode node) => node.parent, ); ``` -------------------------------- ### Instantiate TreeController in Dart Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/README.md Initializes a TreeController, which manages the state of the tree view. It requires the root nodes and a childrenProvider callback to recursively access child nodes. ```dart final treeController = TreeController( roots: roots, childrenProvider: (MyTreeNode node) => node.children, ); ``` -------------------------------- ### Create Hierarchical Data in Dart Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/README.md Demonstrates how to create or fetch hierarchical data using the defined TreeNode model. This involves instantiating the model with data and potentially fetching additional root nodes from a source. ```dart final List roots = [ const MyTreeNode(title: 'My static root node'), ...fetchOtherRootNodes(), ]; ``` -------------------------------- ### Search and Filter Tree Nodes with TreeController Source: https://context7.com/alyssonpp/flutter_tree_view2/llms.txt Demonstrates how to use the `TreeController.search` method to find nodes in a tree structure. It supports both direct and indirect matches and allows for regex-based queries. The `childrenProvider` needs to be configured to respect the filter for accurate results. Rebuilding the controller is necessary to reflect the filtered view. ```dart import 'package:flutter_fancy_tree_view2/flutter_fancy_tree_view2.dart'; class FilterableNode { FilterableNode({required this.title, this.children = const []}); final String title; final List children; } TreeSearchResult? currentFilter; Pattern? searchPattern; // Configure childrenProvider to respect filter Iterable getFilteredChildren(FilterableNode node) { if (currentFilter != null) { return node.children.where(currentFilter!.hasMatch); } return node.children; } final treeController = TreeController( roots: rootNodes, childrenProvider: getFilteredChildren, ); // Perform search void search(String query) { // Reset filter first to ensure all nodes are traversed currentFilter = null; // Create pattern (supports regex) Pattern pattern; try { pattern = RegExp(query, caseSensitive: false); } on FormatException { pattern = query; } searchPattern = pattern; // Execute search currentFilter = treeController.search( (FilterableNode node) => node.title.contains(pattern), ); // Rebuild to show filtered results treeController.rebuild(); // Access search statistics print('Total nodes: ${currentFilter!.totalNodeCount}'); print('Matching nodes: ${currentFilter!.totalMatchCount}'); } // Check if a specific node matches void checkNodeMatch(FilterableNode node) { if (currentFilter == null) return; TreeSearchMatch? match = currentFilter!.matchOf(node); if (match != null) { print('Is direct match: ${match.isDirectMatch}'); print('Subtree node count: ${match.subtreeNodeCount}'); print('Subtree match count: ${match.subtreeMatchCount}'); } // Quick check if node has any match (direct or indirect) bool hasMatch = currentFilter!.hasMatch(node); } // Clear filter void clearSearch() { currentFilter = null; searchPattern = null; treeController.rebuild(); } ``` -------------------------------- ### Integrate Tree Views into Scrollable Layouts with SliverTree Source: https://context7.com/alyssonpp/flutter_tree_view2/llms.txt Provides `SliverTree` and `SliverAnimatedTree` widgets for use within Flutter's `CustomScrollView`. These slivers allow seamless integration of tree structures alongside other scrollable elements like `SliverAppBar` and `SliverToBoxAdapter`. `SliverAnimatedTree` adds animation support for expansion and collapse transitions. ```dart import 'package:flutter/material.dart'; import 'package:flutter_fancy_tree_view2/flutter_fancy_tree_view2.dart'; Widget buildCustomScrollTree() { return CustomScrollView( slivers: [ // Header const SliverAppBar( title: Text('My Tree View'), floating: true, ), // Search bar SliverToBoxAdapter( child: Padding( padding: const EdgeInsets.all(16), child: TextField( decoration: const InputDecoration(hintText: 'Search...'), ), ), ), // Animated tree sliver SliverAnimatedTree( controller: treeController, duration: const Duration(milliseconds: 300), curve: Curves.easeOut, maxNodesToShowWhenAnimating: 50, nodeBuilder: (context, entry) { return TreeIndentation( entry: entry, child: ListTile( title: Text(entry.node.title), onTap: () => treeController.toggleExpansion(entry.node), ), ); }, ), // Footer const SliverToBoxAdapter( child: Padding( padding: EdgeInsets.all(16), child: Text('End of tree'), ), ), ], ); } // Basic sliver tree without animations Widget buildBasicSliverTree() { return CustomScrollView( slivers: [ SliverTree( controller: treeController, nodeBuilder: (context, entry) { return TreeIndentation( entry: entry, child: Text(entry.node.title), ); }, ), ], ); } ``` -------------------------------- ### Build AnimatedTreeView in Flutter Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/README.md Constructs an AnimatedTreeView widget in Flutter, passing the TreeController and defining a nodeBuilder. The nodeBuilder maps tree data to widgets, including logic for toggling expansion and applying indentation. ```dart @override Widget build(BuildContext context) { return AnimatedTreeView( treeController: treeController, nodeBuilder: (BuildContext context, TreeEntry entry) { return InkWell( onTap: () => treeController.toggleExpansion(entry.node), child: TreeIndentation( entry: entry, child: Text(entry.node.title), ), ); }, ); } ``` -------------------------------- ### Import flutter_fancy_tree_view2 in Dart Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/README.md Imports the necessary components from the flutter_fancy_tree_view2 package into your Dart code. This allows you to use the package's widgets and controllers. ```dart import 'package:flutter_fancy_tree_view2/flutter_fancy_tree_view2.dart'; ``` -------------------------------- ### Add flutter_fancy_tree_view2 Dependency in YAML Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/README.md Adds the flutter_fancy_tree_view2 package as a dependency to your Flutter project's pubspec.yaml file. This command automatically fetches the package. ```yaml dependencies: flutter_fancy_tree_view2: any ``` -------------------------------- ### Integrate TreeDraggable and TreeDragTarget Widgets Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/README.md Integrate TreeDraggable and TreeDragTarget into the nodeBuilder to enable drag and drop functionality. TreeDragTarget handles drop events and provides feedback, while TreeDraggable makes the node draggable. The onNodeAccepted callback in TreeDragTarget is where the tree reordering logic should be implemented, followed by a call to treeController.rebuild(). ```dart @override Widget build(BuildContext context) { return AnimatedTreeView( treeController: treeController, nodeBuilder: (BuildContext context, TreeEntry entry) { return TreeDragTarget( node: entry.node, onNodeAccepted: (TreeDragAndDropDetails details) { // Optionally make sure the target node is expanded so the dragging // node is visible in its new vicinity when the tree gets rebuilt. treeController.setExpansionState(details.targetNode, true); // TODO: implement your tree reorder logic // Make sure to rebuild your tree view to show the reordered nodes // in their new vicinity. treeController.rebuild(); }, builder: (BuildContext context, TreeDragAndDropDetails? details) { Widget myTreeNodeTile = Padding( padding: const EdgeInsets.all(8.0), child: Text(entry.node.title), ); // If details is not null, a dragging tree node is hovering this // drag target. Add some decoration to give feedback to the user. if (details != null) { myTreeNodeTile = ColoredBox( color: Theme.of(context).colorScheme.primary.withOpacity(0.3), child: myTreeNodeTile, ); } return TreeDraggable( node: entry.node, // Show some feedback to the user under the dragging pointer, // this can be any widget. feedback: IntrinsicWidth( child: Material( elevation: 4, child: myTreeNodeTile, ), ), child: InkWell( onTap: () => treeController.toggleExpansion(entry.node), child: TreeIndentation( entry: entry, child: myTreeNodeTile, ), ), ); }, ); }, ); } ``` -------------------------------- ### Define TreeNode Model in Dart Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/README.md Defines a simple Dart class to represent a node in the hierarchical data structure. It includes a title and a list of children, enabling the creation of nested data. ```dart class MyTreeNode { const MyTreeNode({ required this.title, this.children = const [], }); final String title; final List children; } ``` -------------------------------- ### Update TreeNode Model for Drag and Drop Source: https://github.com/alyssonpp/flutter_tree_view2/blob/main/README.md Modify the TreeNode model to include a parent reference. This is crucial for the drag and drop feature, especially for auto-expanding/collapsing nodes and correctly managing parent-child relationships during reordering. The parent is set when children are added. ```dart class MyTreeNode { MyTreeNode({ required this.title, Iterable? children, }) : children = [] { if (children == null) return; for (final MyTreeNode child in children) { this.children.add(child); // Make sure to update the parent of your nodes when updating the children // of a given node. child.parent = this; } } final String title; final List children; MyTreeNode? parent; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.