【发布时间】:2022-01-11 03:42:01
【问题描述】:
棘手的问题,希望有人有好的想法。
我在颤动的文本按钮上调用showMenu(...),效果很好。
但是,有时在屏幕调整大小期间,菜单会卡在屏幕上的某个位置(远离其预期位置)。有时它会跟随其锚定位置。很奇怪,我也注意到下拉菜单的这种行为。
这是我的示例代码。我想始终随屏幕移动菜单,或者在最坏的情况下,在屏幕调整大小事件中隐藏菜单。任何关于如何做的想法都会很棒!
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'),
]);
}
}
【问题讨论】:
标签: flutter flutter-web