TreeWidget<T> class
An interactive, scrollable tree widget that displays hierarchical node trees.
It supports custom styles for selected, unselected, and branch connector guide lines.
Keyboard Navigation
- Up Arrow / Down Arrow: Swaps the selected active node upward or downward in the flattened tree list.
- Right Arrow:
- If the active node is a folder and is collapsed, expands it.
- If the active node is already expanded, moves selection down to its first child.
- Left Arrow:
- If the active node is an expanded folder, collapses it.
- If the active node is collapsed or a leaf node, shifts selection back to its parent folder.
- Space / Enter: Triggers the onSelect callback with the active node.
Example Usage
final rootNode = TreeNode(
label: 'Root',
value: 'root_val',
children: [
TreeNode(label: 'Child A', value: 'a_val'),
TreeNode(
label: 'Child B (Folder)',
value: 'b_val',
children: [
TreeNode(label: 'Leaf B1', value: 'b1_val'),
],
),
],
);
TreeWidget(
root: rootNode,
onSelect: (node) {
print('Node chosen: ${node.value}');
},
);
Properties and Settings
| Property | Type | Description |
|---|---|---|
root |
TreeNode | The top-level root node of the tree hierarchy. |
onSelect |
Function(TreeNode) |
Callback executed when a node is activated. |
selectedStyle |
Style | Style applied to the active selected node line. |
unselectedStyle |
Style | Style applied to unselected node labels. |
lineStyle |
Style | Style applied to vertical/horizontal guide lines. |
showRoot |
bool | Whether to display the root node in the tree list. |
focused |
bool | Whether this widget currently has focus. |
Constructors
-
TreeWidget({required TreeNode<
T> root, void onSelect(TreeNode<T> )?, Style selectedStyle = const Style(foreground: CharmColors.pepper, background: CharmColors.charple, modifiers: Modifier.bold), Style unselectedStyle = const Style(foreground: CharmColors.soda), Style lineStyle = const Style(foreground: CharmColors.bbq), bool showRoot = true, bool focused = true}) -
Creates a TreeWidget with the specified
rootnode.
Properties
-
flatNodes
→ List<
FlatNode< T> > -
The list of visible flattened nodes in the tree.
no setter
- focused ↔ bool
-
Whether the tree currently has keyboard focus.
getter/setter pair
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
The optional key for this widget.
finalinherited
- lineStyle ↔ Style
-
The style applied to the horizontal and vertical guide lines.
getter/setter pair
-
onSelect
→ void Function(TreeNode<
T> )? -
Callback executed when a node is selected.
final
-
root
→ TreeNode<
T> -
The root node of the tree.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- selectedIndex ↔ int
-
The index of the currently selected node in the flattened tree.
getter/setter pair
- selectedStyle ↔ Style
-
The style applied to the selected active node.
getter/setter pair
- showRoot ↔ bool
-
Whether to show the root node in the tree.
getter/setter pair
- unselectedStyle ↔ Style
-
The style applied to unselected nodes.
getter/setter pair
Methods
-
adjustScroll(
int viewportHeight) → void -
Adjusts the scroll offset to keep the selected item visible within the
viewportHeight. -
createElement(
) → Element -
Creates an Element to manage this widget's location in the tree.
inherited
-
getIntrinsicHeight(
int width) → int -
Computes the intrinsic height of this widget under the given
widthconstraint.inherited -
handleKeyEvent(
KeyEvent event) → void - Handles incoming key events for navigation and selection.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
render(
Buffer buffer, Rect area) → void -
Renders the widget onto the provided
bufferwithin the specifiedarea.override -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited