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.
Use an ExpandableController
and 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(),
),
);
}
}
You can put the ExpansionPanel inside an ExpansionPanelList and inside it will have a property called expansionCallback
You can wrap ExpansionPanel with ExpansionPanelList, so that you can access to a callback function named expansionCallback . Take a look at the snippet below:
ExpansionPanelList(
animationDuration: const Duration(milliseconds:1000),
children: [
ExpansionPanel(), //..your expansionPanel here
],
expansionCallback: (int item, bool status) {
//status is what you're looking for
},
),
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.