flutterdarteventstileexpansion

What is the equivalent of onExpansionChanged for Flutter ExpandablePanel


I would like to call a fonction when my ExpandablePanel is expanded, with ExpansionTile I do this with onExpansionChanged but here I don't want to use ExpansionTile,

Doesn't anyone have a solution ?

Thanks.


Solution

  • Use an ExpandableControllerand an ExpandableNotifier:

    class MyWidget extends StatefulWidget {
      const MyWidget({Key? key}) : super(key: key);
    
      @override
      State<StatefulWidget> createState() => _MyWidget();
    }
    
    class _MyWidget extends State<MyWidget> {
      final ExpandableController expandableController = ExpandableController();
    
      void onExpandableControllerStateChanged() {
        if (expandableController.expanded) {
          // Do your stuff when panel got expanded here
        } else {
          // Do your stuff when panel got collapsed here
        }
      }
    
      @override
      void initState() {
        super.initState();
        expandableController.addListener(onExpandableControllerStateChanged);
      }
    
      @override
      void dispose() {
        expandableController.removeListener(onExpandableControllerStateChanged);
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return ExpandableNotifier(
          controller: expandableController,
          child: ExpandablePanel(
            header: HeaderWidget(),
            collapsed: CollapsedWidget(),
            expanded: ExpandedWidget(),
          ),
        );
      }
    }