Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand All or scroll to node? #30

Closed
jaxnz opened this issue Aug 3, 2022 · 5 comments
Closed

Expand All or scroll to node? #30

jaxnz opened this issue Aug 3, 2022 · 5 comments
Labels
question Further information is requested

Comments

@jaxnz
Copy link

jaxnz commented Aug 3, 2022

Is it possible to expand all nodes, or scroll to one?

It looks like #7 has been implemented, but can't figure out how it works.

@baumths baumths added the question Further information is requested label Aug 4, 2022
@baumths
Copy link
Owner

baumths commented Aug 4, 2022

Hey @jaxnz, thank your for using the package!

To expand all nodes you can use [TreeViewController.expandAll].

Scrolling to a node is not supported currently, but you could implement it yourself by passing a ScrollController to the treeview and calculating the offset to scroll to using the height and index of a node.

@jaxnz
Copy link
Author

jaxnz commented Aug 4, 2022

Thanks, was able to do it like this for anyone else that may be wondering:

  void scrollTo(TreeNode node) {
    final offset = treeController.indexOf(node) * 30.0;
    scrollController!.animateTo(
      offset,
      duration: const Duration(milliseconds: 500),
      curve: Curves.ease,
    );
  }

@jaxnz jaxnz closed this as completed Aug 4, 2022
@mstich-mcmservices
Copy link

@baumths If we did want to implement our own scroll to node, is there a way to get the height of each of the currently visible tree nodes?

@baumths
Copy link
Owner

baumths commented Oct 9, 2023

Hey @mstich-mcmservices, thank you for using this package!

Checkout this article from the gskinner team about measuring a widget's size:
https://blog.gskinner.com/archives/2021/01/flutter-how-to-measure-widgets.html

Using the RenderBox approach:

class MeasureSizeRenderObject extends RenderProxyBox {
  MeasureSizeRenderObject(this.onChange);
  void Function(Size size) onChange;

  Size? _prevSize;
  @override
  void performLayout() {
    super.performLayout();
    Size newSize = child!.size;
    if (_prevSize == newSize) return;
    _prevSize = newSize;
    WidgetsBinding.instance.addPostFrameCallback((_) => onChange(newSize));
  }
}

class MeasurableWidget extends SingleChildRenderObjectWidget {
  const MeasurableWidget({
    super.key,
    required this.onChange,
    required this.onUnmount,
    required super.child,
  });

  final ValueChanged<Size> onChange;
  final VoidCallback onUnmount;

  @override
  RenderObject createRenderObject(BuildContext context) {
    return MeasureSizeRenderObject(onChange);
  }

  @override
  void didUnmountRenderObject(covariant MeasureSizeRenderObject renderObject) {
    super.didUnmountRenderObject(renderObject);
    onUnmount();
  }
}

Usage example:

// On the StatefulWidget that builds your TreeView
final Map<Object, Size> nodeToSize = {};

Widget nodebuilder(BuildContext context, TreeEntry<Object> entry) {
  return MeasurableWidget(
    onChange: (Size size) {
      nodeToSize[entry.node] = size;
    },
    onUnmount: () {
      nodeToSize.remove(entry.node);
    },
    child: YourNodeTile(entry: entry),
  );
}

After the first frame, nodeToSize.values will have the size of the widgets that are currently built, i.e., viewport + cacheExtent.

@mstich-mcmservices
Copy link

mstich-mcmservices commented Oct 9, 2023

@baumths Thanks for the example code! I will give it a try and see how it works. I started working with your tree package prior to the big 1.x.x rewrite and I'm now in the process of refactoring to handle moving to 1.4.0. Thank you for your great contribution to the flutter community!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants