Tricky question, hoping someone has a nice thought.
I call showMenu(...)
on my text button in flutter, which works well.
However, sometimes during screen resizing, the menu gets stuck somewhere on the screen (away from its intended position). Sometimes it follows its anchored position. Very odd, and I noticed this behavior with the dropdown menu too.
Here is my sample code. I want to either move the menu always with the screen, or in worst case, hide the menu on a screen resizing event. Any thoughts on how to do either would be great!
class ZHomeMenuBar extends StatefulWidget {
const ZHomeMenuBar({Key? key}) : super(key: key);
@override
State<ZHomeMenuBar> createState() => _ZHomeMenuBarState();
}
class _ZHomeMenuBarState extends State<ZHomeMenuBar> {
final GlobalKey _mesKey = GlobalKey();
final GlobalKey _accKey = GlobalKey();
@override
Widget build(BuildContext context) {
final zuser = Provider.of<ZUser?>(context);
return Container(
height: 66,
decoration: BoxDecoration(color: context.backgroundColor),
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Row(
children: [
Text(zuser == null ? "" : zuser.displayName ?? ""),
const Spacer(),
ZTextButton(text: "Portfolio", onPressed: () => {}),
context.sh,
ZTextButton(
key: _mesKey,
text: "Messages",
onPressed: () {
_showMessage(context);
}),
context.sh,
ZTextButton(key: _accKey, text: "Account", onPressed: () {}),
context.sh,
],
),
);
}
_showMessage(context) {
final RenderBox renderBox =
_mesKey.currentContext?.findRenderObject() as RenderBox;
final Size size = renderBox.size;
final Offset offset = renderBox.localToGlobal(Offset.zero);
showMenu(
context: context,
position: RelativeRect.fromLTRB(offset.dx, offset.dy + size.height,
offset.dx + size.width, offset.dy + size.height),
items: [
PopupMenuItem<String>(child: const Text('menu option 1'), value: '1'),
PopupMenuItem<String>(child: const Text('menu option 2'), value: '2'),
PopupMenuItem<String>(child: const Text('menu option 3'), value: '3'),
]);
}
}