You can pass the function callback to the StatelessWidget during initialization. That's my approach on initializing an ActionMenu that calls its functions from the parent class.
late ActionMenu actionMenu;
@override
void initState() {
super.initState();
// Initialize ActionMenu
// arguments passed are Home Screen functions
// that can be in ActionMenu class
actionMenu = ActionMenu(this.settings, this.logout);
}
settings(){
// TODO
}
logout(){
// TODO
}
...and on the ActionMenu. You can now call the functions inside the StatelessWidget.
enum MenuOptions { menuSettings, menuLogout }
class ActionMenu extends StatelessWidget {
final Function menuSettings, menuLogout;
ActionMenu(this.menuSettings, this.menuLogout);
@override
Widget build(BuildContext context) {
return PopupMenuButton<MenuOptions>(
onSelected: (MenuOptions value) {
switch (value) {
case MenuOptions.menuSettings:
this.menuSettings();
break;
case MenuOptions.menuLogout:
this.menuLogout();
break;
}
},
itemBuilder: (BuildContext context) => <PopupMenuItem<MenuOptions>>[
PopupMenuItem<MenuOptions>(
value: MenuOptions.menuSettings,
child: Text('Settings'),
),
PopupMenuItem<MenuOptions>(
value: MenuOptions.menuLogout,
child: Text('Logout'),
),
],
);
}
}