当我把光标放在弹出按钮上时,我试图显示我的弹出项,并使用Global键进行了显示。但问题是,我试图对我的其他弹出按钮做同样的事情,但当我这样做时,一切都出了问题。错误是我不能为多个小部件使用全局密钥。下面你可以看到我正在使用的全局密钥和我的小部件。所以,如果你知道如何解决这个问题,请在这里告诉我。
GlobalKey popUpButtonKey = GlobalKey();
openPopUpItem() {
GestureDetector? detector;
searchForGestureDetector(BuildContext element) {
element.visitChildElements((element) {
if (element.widget != null && element.widget is GestureDetector) {
detector = element.widget as GestureDetector;
} else {
searchForGestureDetector(element);
}
});
}
searchForGestureDetector(popUpButtonKey.currentContext!);
detector!.onTap!();
}
body: ListView(
children: [
Row(
children: <Widget>[
const SizedBox(
width: 20,
),
SizedBox(
height: 80,
width: 185,
child: Image.asset('assets/images/logo2.png'),
),
Spacer(),
if (!Responsive.isMobile(context))
PopupMenuButton(
tooltip: '',
child: Text(
'Escorts',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[]),
Padding(padding: EdgeInsets.all(10.0)),
if (!Responsive.isMobile(context))
InkWell(
onHover: (value) {
if (value) openPopUpItem();
},
onTap: () {},
child: PopupMenuButton(
key: popUpButtonKey2,
tooltip: '',
color: Color(0xFF262533),
position: PopupMenuPosition.under,
child: Text(
'Agenturen & Clubs',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[
const PopupMenuItem(
child: ListTile(
title: Text(
'Escortagenturen',
style: TextStyle(color: Colors.white),
),
),
),
const PopupMenuItem(
child: ListTile(
title: Text(
'Bordelle',
style: TextStyle(color: Colors.white),
),
),
),
const PopupMenuItem(
child: ListTile(
title: Text(
'Laufhauser',
style: TextStyle(color: Colors.white),
),
),
),
const PopupMenuItem(
child: ListTile(
title: Text(
'Saunaclubs',
style: TextStyle(color: Colors.white),
),
),
),
const PopupMenuItem(
child: ListTile(
title: Text(
'Domina & BDSM-Studios',
style: TextStyle(color: Colors.white),
),
),
),
const PopupMenuItem(
child: ListTile(
title: Text(
'Tantra & Massaage-Studios',
style: TextStyle(color: Colors.white),
),
),
),
]),
),
Padding(padding: EdgeInsets.all(10.0)),
if (!Responsive.isMobile(context))
PopupMenuButton(
tooltip: '',
child: Text(
'Inserieren',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[]),
Padding(padding: EdgeInsets.all(10.0)),
if (!Responsive.isMobile(context))
InkWell(
onHover: (value) {
if (value) openPopUpItem();
},
onTap: () {},
child: PopupMenuButton(
key: popUpButtonKey,
tooltip: '',
color: Color(0xFF262533),
position: PopupMenuPosition.under,
child: Text(
'Werben',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[
const PopupMenuItem(
child: ListTile(
title: Text(
'Werbenformate',
style: TextStyle(color: Colors.white),
),
),
),
const PopupMenuItem(
child: ListTile(
title: Text(
'Preise',
style: TextStyle(color: Colors.white),
),
),
),
]),
),
也许您可以尝试向openPopUpItem
添加一个参数,如下所示:
openPopUpItem(Key widgetKey) { //added argument
GestureDetector? detector;
searchForGestureDetector(BuildContext element) {
element.visitChildElements((element) {
if (element.widget != null && element.widget is GestureDetector) {
detector = element.widget as GestureDetector;
} else {
searchForGestureDetector(element);
}
});
}
searchForGestureDetector(widgetKey.currentContext!); //changed to use the key passed, not an hardcoded one
detector!.onTap!();
}