Skip to content
This repository was archived by the owner on Mar 28, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/lib/src/examples/drag_and_drop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class _DragAndDropTreeViewState extends State<DragAndDropTreeView> {
populateExampleTree(root);

treeController = TreeController<Node>(
roots: root.children,
root: root,
childrenProvider: (Node node) => node.children,

// The parentProvider is extremely important when automatically expanding
Expand Down
2 changes: 1 addition & 1 deletion example/lib/src/examples/filterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class _FilterableTreeViewState extends State<FilterableTreeView> {
populateExampleTree(root);

treeController = TreeController<Node>(
roots: root.children,
root: root,
childrenProvider: getChildren,
)..expandAll();

Expand Down
2 changes: 1 addition & 1 deletion example/lib/src/examples/lazy_loading.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class _LazyLoadingTreeViewState extends State<LazyLoadingTreeView> {
void initState() {
super.initState();
treeController = TreeController<Data>(
roots: childrenProvider(Data.root),
root: Data.root,
childrenProvider: childrenProvider,
);
}
Expand Down
2 changes: 1 addition & 1 deletion example/lib/src/examples/minimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class _MinimalTreeViewState extends State<MinimalTreeView> {
});

treeController = TreeController<Node>(
roots: root.children,
root: root,
childrenProvider: (Node node) => node.children,
);
}
Expand Down
15 changes: 13 additions & 2 deletions lib/src/drag_and_drop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TreeDraggable<T extends Object> extends StatefulWidget {
this.hitTestBehavior = HitTestBehavior.deferToChild,
this.longPressDelay,
this.longPressHapticFeedbackOnStart = true,
this.maxSimultaneousDrags = 1,
});

/// The widget below this widget in the tree.
Expand Down Expand Up @@ -256,6 +257,16 @@ class TreeDraggable<T extends Object> extends StatefulWidget {
/// Defaults to `null`.
final Duration? longPressDelay;

/// How many simultaneous drags to support.
///
/// When null, no limit is applied. Set this to 1 if you want to only allow
/// the drag source to have one item dragged at a time. Set this to 0 if you
/// want to prevent the draggable from actually being dragged.
///
/// If you set this property to 1, consider supplying an "empty" widget for
/// [childWhenDragging] to create the illusion of actually moving [child].
final int? maxSimultaneousDrags;

@override
State<TreeDraggable<T>> createState() => _TreeDraggableState<T>();
}
Expand Down Expand Up @@ -378,7 +389,7 @@ class _TreeDraggableState<T extends Object> extends State<TreeDraggable<T>>
if (widget.longPressDelay != null) {
return LongPressDraggable<T>(
data: widget.node,
maxSimultaneousDrags: 1,
maxSimultaneousDrags: widget.maxSimultaneousDrags,
onDragStarted: onDragStarted,
onDragUpdate: onDragUpdate,
onDraggableCanceled: onDraggableCanceled,
Expand All @@ -399,7 +410,7 @@ class _TreeDraggableState<T extends Object> extends State<TreeDraggable<T>>

return Draggable<T>(
data: widget.node,
maxSimultaneousDrags: 1,
maxSimultaneousDrags: widget.maxSimultaneousDrags,
onDragStarted: onDragStarted,
onDragUpdate: onDragUpdate,
onDraggableCanceled: onDraggableCanceled,
Expand Down
20 changes: 12 additions & 8 deletions lib/src/tree_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ class TreeController<T extends Object> with ChangeNotifier {
/// [TreeController.parentProvider] is set to a callback that always returns
/// null.
TreeController({
required Iterable<T> roots,
required T root,
required this.childrenProvider,
ParentProvider<T>? parentProvider,
this.defaultExpansionState = false,
}) : _roots = roots {
}) : _root = root {
assert(() {
_debugHasParentProvider = parentProvider != null;
return true;
Expand All @@ -112,11 +112,16 @@ class TreeController<T extends Object> with ChangeNotifier {
/// The roots of the tree.
///
/// These nodes are used as a starting point when traversing the tree.
Iterable<T> get roots => _roots;
Iterable<T> _roots;
set roots(Iterable<T> nodes) {
if (nodes == _roots) return;
_roots = nodes;
Iterable<T> get roots => childrenProvider(root);

/// The root of the tree.
///
/// This node is used as a starting point when traversing the tree.
T get root => _root;
T _root;
set root(T node) {
if (node == _root) return;
_root = node;
rebuild();
}

Expand Down Expand Up @@ -615,7 +620,6 @@ class TreeController<T extends Object> with ChangeNotifier {

@override
void dispose() {
_roots = const Iterable.empty();
toggledNodes.clear();
super.dispose();
}
Expand Down
9 changes: 8 additions & 1 deletion lib/src/tree_indentation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class IndentGuide {
PathModifier? pathModifier,
bool roundCorners,
bool connectBranches,
double? fixedVerticalOffset,
}) = ConnectingLinesGuide;

/// Convenient constructor to create a [ScopingLinesGuide].
Expand Down Expand Up @@ -522,6 +523,7 @@ class ConnectingLinesGuide extends AbstractLineGuide {
super.pathModifier,
this.roundCorners = false,
this.connectBranches = false,
this.fixedVerticalOffset,
});

/// Decides if the connection between vertical and horizontal lines should be
Expand All @@ -543,6 +545,9 @@ class ConnectingLinesGuide extends AbstractLineGuide {
/// Defaults to `false`.
final bool connectBranches;

/// TODO
final double? fixedVerticalOffset;

@override
CustomPainter createPainter(BuildContext context, TreeEntry<Object> entry) {
return _ConnectingLinesPainter(
Expand All @@ -567,6 +572,7 @@ class ConnectingLinesGuide extends AbstractLineGuide {
PathModifier? Function()? pathModifier,
bool? roundCorners,
bool? connectBranches,
double? fixedVerticalOffset,
}) {
return ConnectingLinesGuide(
indent: indent ?? this.indent,
Expand All @@ -580,6 +586,7 @@ class ConnectingLinesGuide extends AbstractLineGuide {
pathModifier: pathModifier != null ? pathModifier() : this.pathModifier,
roundCorners: roundCorners ?? this.roundCorners,
connectBranches: connectBranches ?? this.connectBranches,
fixedVerticalOffset: fixedVerticalOffset ?? fixedVerticalOffset,
);
}

Expand Down Expand Up @@ -671,7 +678,7 @@ class _ConnectingLinesPainter extends CustomPainter {
}

// Add connections
final double y = size.height * 0.5;
final double y = guide.fixedVerticalOffset ?? size.height * 0.5;
path.moveTo(connectionStart, 0.0);

if (guide.roundCorners) {
Expand Down