configurable_expansion_tile icon indicating copy to clipboard operation
configurable_expansion_tile copied to clipboard

Needed to call handleTap() from outside function

Open Skquark opened this issue 6 years ago • 4 comments

First off, this widget saved the day, the standard material ExpansionTileList was way limited and not configurable at all, so big thank you.. It would be so easy for them to add more options into the built in one, but they went for material spec only I guess. Anyways, for my use case, I needed to toggle the tile manually from my own functions and had some difficulty getting there. Using it for a Comments panel with the likes and posts, and we wanted to open it on a long press, then have the Hide Comments button on the bottom of the expanded panel instead of just the Header. I put the ConfigurableExpansionTile in it's own variable, but when calling it, there was no way to manually toggle it, so I took the liberty of hacking your code a bit, and wanted to share.. Needed to make void _handleTap public, so got rid of the underscore inside the State, but that wasn't enough to access it. Inside the StatefulWidget class, I changed the createState part to this:

  static final Animatable<double> _easeOutTween =
      CurveTween(curve: Curves.easeOut);
// Modified here:
  final _ConfigurableExpansionTileState configurableExpansionTile = _ConfigurableExpansionTileState();

  void handleTap() {
    configurableExpansionTile.handleTap();
  }

  @override
  _ConfigurableExpansionTileState createState() => configurableExpansionTile;
      //_ConfigurableExpansionTileState();
}

Easy enough to do, taught myself something, works good now. I can call it from my own GestureDetector with commentTile.handleTap(); like I wanted. It might be nice to add this in your next version release. Thanks for the needed component, think I'm gonna have a few more usages for it.

Skquark avatar Dec 18 '18 11:12 Skquark

Actually, that was working at first, but sometimes when I call it to close the panel I get this exception triggered from inside the handleTap:

FlutterError (setState() called in constructor: _ConfigurableExpansionTileState#181ba(lifecycle state: created, no widget, not mounted) This happens when you call setState() on a State object for a widget that hasn't been inserted into the widget tree yet. It is not necessary to call setState() in the constructor, since the state is already assumed to be dirty when it is initially created.)

Thought I was doing it the right way, but there's a tricky flutter thing going on that I'm trying to figure out. I'll play with it some more, but that error wasn't a common one.. If you might know, let me know. Thanks.

Skquark avatar Dec 18 '18 13:12 Skquark

Hey there, you can turn the state and the handleTap public and create a globalKey referring this state, like this: final GlobalKey<ConfigurableExpansionTileState> expansionTitleGlobalKey = GlobalKey();

and call on your action like this: onPressed: () { expansionTitleGlobalKey.currentState.handleTap(); },

and you must reference this key on your Widget:

ConfigurableExpansionTile( key: expansionTitleGlobalKey,.....

vinsalmont avatar Jan 03 '19 16:01 vinsalmont

@Skquark Thanks for the suggestion, it's definitely I should add. Did the suggestion from @vinsalmont help work for you?

matthewstyler avatar Jan 04 '19 20:01 matthewstyler

I put some buttons to children. How i can close after click by any button in children?

DrobyshevAlex avatar May 25 '20 10:05 DrobyshevAlex