Tahaan / expandable_tree_menu

Flutter Widget allowing you to easily create a tree-structure "menu" from a recursive list of data nodes
BSD 3-Clause "New" or "Revised" License
1 stars 2 forks source link

Expand/close nodes using onSelect() functions #2

Open SupremeDeity opened 3 years ago

SupremeDeity commented 3 years ago

Is there some way to be able to expand/close nodes using the onSelect() function?

juanpsama commented 2 months ago

I think you can't but i was able to pass a function as onSelect but when the node expand, an onExpand callback, here's how i do it

typedef NodeSelectedCallback<T> = void Function(T value);
typedef NodeExpandedCallback<T> = void Function(T value, bool isExpanded);

typedef NodeBuilder<T> = Widget Function(BuildContext context, T value);

// TODO: Consider allowing a custom "SubTree" node, eg Flutter "ExpansionTile"

/// Wrapper for node with children / sub-items.
class SubTreeWrapper<T> extends StatefulWidget {
  final NodeSelectedCallback<T>? onSelect;
  final NodeExpandedCallback<T>? onExpand;

you've to pass another parameter to the widget and in this case i add to send isExpanded parameter, every node in the tree is a ExpansionTile and this receives a onExpansionChangued parameter as so

@override
  Widget build(BuildContext context) {
    return Material(
      // color: Colors.transparent,
      child: Container(
        margin: widget.submenuMargin,
        decoration: widget.submenuDecoration,
        child: ExpansionTile(
          collapsedBackgroundColor: widget.submenuClosedColor,
          backgroundColor: widget.submenuOpenColor,
          onExpansionChanged: toggleState,

and you can pass you new callback on the toggleState method

void toggleState(
    bool isExpanded,
  ) {
    setState(() {
      twistyState = isExpanded ? TwistyState.open : TwistyState.closed;
      widget.onExpand!(widget.node.value, isExpanded);
    });
  }

and this onExpanded callback should be in the ExpandableTree widget

 const CustomExpandableTree({
    Key? key,
    this.onSelect,
    this.onExpand,

for any other customization you should also see the ExpansionTile class documentation