SliverTree<TKey, TData> class

A sliver that displays a tree structure with support for expand/collapse animations.

The tree data and state are managed by TreeController. The widget rebuilds when the controller notifies listeners (e.g., on animation tick or expand/collapse).

Children are built lazily using nodeBuilder only when they become visible in the viewport.

Example:

class _MyTreeState extends State<MyTree> with TickerProviderStateMixin {
  late final TreeController<String, String> _controller;

  @override
  void initState() {
    super.initState();
    _controller = TreeController<String, String>(vsync: this);
    _controller.setRoots([
      TreeNodeData(id: '1', data: 'Root 1'),
      TreeNodeData(id: '2', data: 'Root 2'),
    ]);
    _controller.setChildren('1', [
      TreeNodeData(id: '1.1', data: 'Child 1.1'),
      TreeNodeData(id: '1.2', data: 'Child 1.2'),
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverTree<String, String>(
          controller: _controller,
          nodeBuilder: (context, nodeKey, depth) {
            final data = _controller.getNodeData(nodeKey)!.data;
            return ListTile(
              title: Text(data),
              leading: Icon(Icons.folder),
            );
          },
        ),
      ],
    );
  }
}
Inheritance

Constructors

SliverTree({required TreeController<TKey, TData> controller, required Widget nodeBuilder(BuildContext context, TKey nodeKey, int nodeDepth), int maxStickyDepth = 0, Key? key})
Creates a sliver tree.
const

Properties

controller TreeController<TKey, TData>
Controller that manages tree state and animations.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
maxStickyDepth int
Maximum depth of nodes that become sticky headers.
final
nodeBuilder Widget Function(BuildContext context, TKey nodeKey, int nodeDepth)
Builder function that creates a widget for each visible tree node.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

createElement() SliverTreeElement<TKey, TData>
RenderObjectWidgets always inflate to a RenderObjectElement subclass.
override
createRenderObject(BuildContext context) RenderSliverTree<TKey, TData>
Creates an instance of the RenderObject class that this RenderObjectWidget represents, using the configuration described by this RenderObjectWidget.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
didUnmountRenderObject(covariant RenderObject renderObject) → void
This method is called when a RenderObject that was previously associated with this widget is removed from the render tree. The provided RenderObject will be of the same type as the one created by this widget's createRenderObject method.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited
updateRenderObject(BuildContext context, covariant RenderSliverTree<TKey, TData> renderObject) → void
Copies the configuration described by this RenderObjectWidget to the given RenderObject, which will be of the same type as returned by this object's createRenderObject.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited