I have a list of items. Each item has a button to show a sub-menu to do some action.
I want to show only a sub-menu and hide others. When I tap an IconButton
it shows and hides a sub-menu and works correctly but when another item sub-menu is open and I tap the other one I can not handle hiding the other opened sub-menu and opening the newly tapped one.
Here is the list builder
class List extends StatelessWidget {
const List({super.key, this.items});
final List<ListItem> items;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
return ItemListTile(
miner: items[index],
);
},
itemCount: items.length,
);
}
}
Here is the ItemListTile
code:
class ItemListTile extends StatelessWidget {
const ItemListTile({super.key, this.item});
final Item item;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text('title'),
trailing: IconButton(
onPressed: () {
// How to toggle menus open and close state?
if (isOpen) {
closeMenu();
} else {
openMenu();
}
},
icon: const Icon(
Icons.more_vert_outlined,
color: Colors.white,
),
color: Colors.white,
),
);
}
void openMenu() {
// How to handle state?
findButton();
overlayEntry = _overlayEntryBuilder();
Overlay.of(context, debugRequiredFor: widget).insert(overlayEntry!);
}
void closeMenu() {
// How to handle state?
overlayEntry?.remove();
}
@override
void dispose() {
overlayEntry?.remove();
overlayEntry = null;
super.dispose();
}
}
I want to learn to solve this problem using stateful widgets or Bloc pattern. Thanks in advance.