Related
I tried to show my popupitems when I put my cursor over my Popupbutton and I made it by using Global key. But the problem is that i tried to do the same thing to my other popupbuttons but when I did this, everything went wrong. The error was that I can not use a global key for more than one widget. Below You can see my global key that I'm using and my widgets. So if you have any idea how to fix this, please let me know here.
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),
),
),
),
]),
),
Maybe you can try adding an argument to openPopUpItem, like this:
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!();
}
I want to build a dropdown menu that shows up as a popup when I put my cursor in my icon for example. I have attached a picture below on how I want to be displayed. I have also wrote my code below as an example what I tried to do, although I know its not a good solution. So does anyone knows how can I fix this. Click "image" to see how I want to be fixed
import 'package:flutter/material.dart';
import 'package:prove/responsive/responsive.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
#override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
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!();
}
#override
Widget build(BuildContext context) {
return Scaffold(
endDrawer: Drawer(
backgroundColor: const Color(0xFF262533),
elevation: 10,
child: Padding(
padding: const EdgeInsets.all(18.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 30,
),
PopupMenuButton(
tooltip: '',
child: Text(
'Escorts',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[]),
Padding(padding: EdgeInsets.all(10.0)),
PopupMenuButton(
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)),
PopupMenuButton(
tooltip: '',
child: Text(
'Inserieren',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[]),
Padding(padding: EdgeInsets.all(10.0)),
PopupMenuButton(
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),
),
),
),
]),
Padding(padding: EdgeInsets.all(10.0)),
PopupMenuButton(
tooltip: '',
color: Color(0xFF262533),
position: PopupMenuPosition.under,
child: Text(
'Blog',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[
const PopupMenuItem(
child: ListTile(
title: Text(
'Archiv',
style: TextStyle(color: Colors.white),
),
),
),
]),
const Padding(
padding: EdgeInsets.all(10.0),
),
PopupMenuButton(
position: PopupMenuPosition.under,
tooltip: '',
child: const Text(
'Kontakt',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[]),
],
),
),
),
backgroundColor: const Color(0xFF262533),
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))
PopupMenuButton(
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))
PopupMenuButton(
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),
),
),
),
]),
Padding(padding: EdgeInsets.all(10.0)),
if (!Responsive.isMobile(context))
PopupMenuButton(
tooltip: '',
color: Color(0xFF262533),
position: PopupMenuPosition.under,
child: Text(
'Blog',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[
const PopupMenuItem(
child: ListTile(
title: Text(
'Archiv',
style: TextStyle(color: Colors.white),
),
),
),
]),
const Padding(
padding: EdgeInsets.all(10.0),
),
if (!Responsive.isMobile(context))
PopupMenuButton(
position: PopupMenuPosition.under,
tooltip: '',
child: const Text(
'Kontakt',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[]),
Spacer(),
InkWell(
onHover: (value) {
if (value) openPopUpItem();
},
onTap: () {},
child: PopupMenuButton(
key: popUpButtonKey,
color: Color(0xFF262533),
tooltip: '',
position: PopupMenuPosition.under,
child: Icon(
Icons.person,
color: Colors.white,
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[
const PopupMenuItem(
child: ListTile(
title: Text(
'Login',
style: TextStyle(color: Colors.white),
),
),
),
const PopupMenuItem(
child: ListTile(
title: Text(
'Register',
style: TextStyle(color: Colors.white),
),
),
),
],
),),
const Padding(
padding: EdgeInsets.all(10.0),
),
PopupMenuButton(
color: Color(0xFF262533),
tooltip: '',
position: PopupMenuPosition.under,
child: Icon(
Icons.search,
color: Colors.white,
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[
PopupMenuItem(
child: ListTile(
title: TextField(
decoration: InputDecoration(
suffixIcon: Container(
decoration: BoxDecoration(
color: Colors.pink,
borderRadius: BorderRadius.all(
Radius.circular(8.0))),
child: new IconButton(
style: IconButton.styleFrom(
shape: CircleBorder()),
icon: new Icon(
Icons.arrow_right,
color: Colors.white,
),
onPressed: (() {}),
iconSize: 25,
),
),
filled: true, //<-- SEE HERE
fillColor: Colors.white,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(
width: 1,
color: Color(0xFFE51A72),
),
),
hintText: 'Suchen...',
hintStyle: TextStyle(fontSize: 15),
),
),
),
),
]),
SizedBox(
width: 20,
),
if (!Responsive.isDesktop(context))
Builder(
builder: (context) => IconButton(
onPressed: () {
Scaffold.of(context).openEndDrawer();
},
icon: Icon(
Icons.menu,
color: Colors.white,
size: 30,
))),
],
)
],
),
);
}
}
I am using Global key to find the widget.
openPopUpItem() {
GestureDetector? detector;
searchForGestureDetector(BuildContext element) {
element.visitChildElements((element) {
if (element.widget is GestureDetector) {
detector = element.widget as GestureDetector;
} else {
searchForGestureDetector(element);
}
});
}
searchForGestureDetector(popUpButtonKey.currentContext!);
detector!.onTap!();
}
class PopUpMenuTest extends StatefulWidget {
const PopUpMenuTest({super.key});
#override
State<PopUpMenuTest> createState() => _PopUpMenuTestState();
}
class _PopUpMenuTestState extends State<PopUpMenuTest> {
GlobalKey popUpButtonKey = GlobalKey();
openPopUpItem() {
GestureDetector? detector;
searchForGestureDetector(BuildContext element) {
element.visitChildElements((element) {
if (element.widget is GestureDetector) {
detector = element.widget as GestureDetector;
} else {
searchForGestureDetector(element);
}
});
}
searchForGestureDetector(popUpButtonKey.currentContext!);
detector!.onTap!();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
InkWell(
onHover: (value) {
if (value) openPopUpItem();
},
onTap: () {},
child: PopupMenuButton(
key: popUpButtonKey,
color: Color(0xFF262533),
tooltip: '',
position: PopupMenuPosition.under,
child: Icon(
Icons.person,
// color: Colors.white,
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[
const PopupMenuItem(
child: ListTile(
title: Text(
'Login',
style: TextStyle(color: Colors.white),
),
),
),
const PopupMenuItem(
child: ListTile(
title: Text(
'Register',
style: TextStyle(color: Colors.white),
),
),
),
],
),
)
],
),
);
}
}
I build a drawer as in the image using Flutter, but the elements of the drawer are showed in the left side and its not beautiful like this. I would like to show them in the center of the drawer or at least a little more to the right. Below you can find my code as a reference. It would be great if someone here can give a little help.
img
return Scaffold(
endDrawer: Drawer(backgroundColor: const Color(0xFF262533),
elevation: 10,
child: ListView(
children: [
PopupMenuButton(
tooltip: '',
child: Text(
'Escorts',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) =>
<PopupMenuEntry>[]),
Padding(padding: EdgeInsets.all(10.0)),
PopupMenuButton(
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),
),
),
),
]),
PopupMenuButton(
tooltip: '',
child: Text(
'Inserieren',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) =>
<PopupMenuEntry>[]),
Padding(padding: EdgeInsets.all(10.0)),
PopupMenuButton(
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),
),
),
),
]),
Padding(padding: EdgeInsets.all(10.0)),
PopupMenuButton(
tooltip: '',
color: Color(0xFF262533),
position: PopupMenuPosition.under,
child: Text(
'Blog',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) =>
<PopupMenuEntry>[
const PopupMenuItem(
child: ListTile(
title: Text(
'Archiv',
style:
TextStyle(color: Colors.white),
),
),
),
]),
const Padding(
padding: EdgeInsets.all(10.0),
),
PopupMenuButton(
position: PopupMenuPosition.under,
tooltip: '',
child: const Text(
'Kontakt',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
itemBuilder: (BuildContext context) =>
<PopupMenuEntry>[]),
],
),
),
replace ListView with Column widget
and inside Column make
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
code:
endDrawer: Drawer(
backgroundColor: const Color(0xFF262533),
elevation: 10,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
//todo
]
))
you can align your element by use Container or Column in my case I build drawer like this
Drawer(
backgroundColor: black,
child: Column(
children: [
SizedBox(
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: black,
borderRadius: BorderRadius.circular(10),
border: Border.all(
color: accent,
width: 0.5,
),
),
height: MediaQuery.of(context).size.width / 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
"assets/logs.png",
height: 80,
width: 80,
),
const Text(
"Name App",
style: TextStyle(
color: grey,
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
const Text(
"some body text",
style: TextStyle(
color: white,
fontSize: 14,
),
),
],
)),
),
const SizedBox(
height: 10,
),
Expanded(
child: ListView(
children: [
const Padding(
padding: EdgeInsets.only(right: 10),
child: Text(
"body text",
style: TextStyle(
color: white,
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
Padding(
padding: const EdgeInsets.only(right: 15),
child: Row(
children: const [
Icon(
Icons.check_circle_outline_outlined,
size: 20,
color: accent,
),
SizedBox(
width: 10,
),
Text(
"some body text",
style: TextStyle(
color: grey,
fontSize: 14,
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(right: 15, top: 4),
child: Row(
children: const [
Icon(
Icons.check_circle_outline_outlined,
color: accent,
size: 20,
),
SizedBox(
width: 10,
),
Text(
"some body text",
style: TextStyle(
color: grey,
fontSize: 14,
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(right: 15, top: 4),
child: Row(
children: const [
Icon(
Icons.check_circle_outline_outlined,
size: 20,
color: accent,
),
SizedBox(
width: 10,
),
Text(
"some body text",
style: TextStyle(
color: grey,
fontSize: 14,
),
),
],
),
),
const Divider(
endIndent: 10,
indent: 10,
color: grey,
),
ListTile(
dense: true,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const AboutPage()));
},
leading: const Icon(
Icons.info_outlined,
color: accent,
),
title: const Text(
"About App",
style: TextStyle(
color: white,
fontSize: 14,
),
),
trailing: const Icon(
Icons.arrow_back_ios_new,
color: grey,
size: 15,
),
),
ListTile(
dense: true,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const AboutMe()));
},
leading: const Icon(
Icons.engineering,
color: accent,
),
title: const Text(
"About",
style: TextStyle(
color: white,
fontSize: 14,
),
),
trailing: const Icon(
Icons.arrow_back_ios_new,
color: grey,
size: 15,
),
),
],
),
),
Container(
padding: const EdgeInsets.only(left: 8, right: 8),
child: const Text(
"Folow me Now !",
style: TextStyle(fontSize: 12, color: grey),
textAlign: TextAlign.center,
)),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: black,
),
margin: const EdgeInsets.all(10),
child: Row(
children: [
const Spacer(),
IconButton(
onPressed: () {
},
icon: const Icon(
FontAwesomeIcons.facebook,
color: accent,
),
),
IconButton(
onPressed: () {
},
icon: const Icon(
FontAwesomeIcons.instagram,
color: accent,
),
),
IconButton(
onPressed: () {},
icon: const Icon(
FontAwesomeIcons.whatsapp,
color: accent,
),
),
IconButton(
onPressed: () {},
icon: const Icon(
FontAwesomeIcons.telegram,
color: accent,
),
),
const Spacer(),
],
),
),
],
),
),
I want to build a menu bar in Flutter for my web app. I want to show my popupmenuitems as a popup when I hover the button. Is this possible?
PopupMenuButton(
tooltip: '',
color: const Color(0xFF262533),
position: PopupMenuPosition.under,
child: const 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),
),
),
),
]),
InkWell(
onHover: (value) {
// show your popup
},
child: Text(
'Agenturen & Clubs',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontFamily: 'Poppins',
),
),
),
How do I make my cart the same when accessed from all pages?
I have different categories of drinks from different classes and the items from those classes can all be added to the cart but when I click the cart icon on the appbar from the page that i am in, I only see the items from the page that I am in and not the icons from all the different categories
The cart Page
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dish_object.dart';
class Cart extends StatefulWidget {
final List<Drinks> _cart;
Cart(this._cart);
#override
_CartState createState() => _CartState(this._cart);
}
class _CartState extends State<Cart> {
_CartState(this._cart);
List<Drinks> _cart;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.redAccent[100],
title: Text(
'Cart',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.normal),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.send_rounded),
tooltip: "Confirm Order",
onPressed: () {
if (_cart.isNotEmpty) {
setState(() {
Fluttertoast.showToast(
msg: "Order Confirmed",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.green[200],
textColor: Colors.white,
fontSize: 16.0);
});
}
if (_cart.isEmpty) {
setState(() {
Fluttertoast.showToast(
msg: "Cart Empty",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
});
}
}),
if (_cart.length > 0)
Padding(
padding: const EdgeInsets.only(right: 5.0, bottom: 10.0),
child: CircleAvatar(
radius: 10.0,
backgroundColor: Colors.red,
foregroundColor: Colors.white,
child: Text(
_cart.length.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 12.0,
),
),
),
),
],
),
body: ListView.builder(
itemCount: _cart.length + 1,
itemBuilder: (context, index) {
if (index == _cart.length)
return Align(
alignment: Alignment.bottomLeft,
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 10.0, vertical: 2.0),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
),
elevation: 4.0,
child: Text(
"Cart Total: R" +
_cart
.fold<int>(0, (a, b) => a + b.totalPrice())
.toString(),
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 25,
color: Colors.redAccent[200])),
),
),
);
var item = _cart[index];
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 1.0),
child: Card(
elevation: 4.0,
child: ListTile(
//Leading
leading: Text(
item.category + "\n" + "R" + item.price.toString(),
),
//Title
title: Text(item.brandName +
"\n" +
"(" +
item.counter.toString() +
")"),
//Subtitle
subtitle: GestureDetector(
child: Icon(
Icons.add,
color: Colors.green,
),
onTap: () {
setState(() {
item.incrementCounter();
});
},
),
//Trailing
trailing: GestureDetector(
child: Icon(
Icons.remove_circle,
color: Colors.red,
),
onTap: () {
setState(() {
item.decrementCounter();
});
},
),
isThreeLine: true,
),
),
);
},
),
);
}
}
The Gin Cocktails category class
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'cart.dart';
import 'dish_object.dart';
import 'main_drawer.dart';
class ginCocktails extends StatefulWidget {
ginCocktails({Key key, this.title}) : super(key: key);
final String title;
#override
_ginCocktailsState createState() => _ginCocktailsState();
}
class _ginCocktailsState extends State<ginCocktails> {
List<Drinks> _dishes = List<Drinks>();
List<Drinks> _cartList = List<Drinks>();
#override
void initState() {
super.initState();
_populateDishes();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Gin Cocktails"),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 16.0, top: 8.0),
child: GestureDetector(
child: Stack(
alignment: Alignment.topCenter,
children: <Widget>[
Icon(
Icons.shopping_basket,
size: 35.0,
),
if (_cartList.length > 0)
Padding(
padding: const EdgeInsets.only(left: 2.0),
child: CircleAvatar(
radius: 8.0,
backgroundColor: Colors.red,
foregroundColor: Colors.white,
child: Text(
_cartList.length.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 12.0,
),
),
),
),
],
),
onTap: () {
if (_cartList.isNotEmpty)
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Cart(_cartList),
),
);
setState(() {
if (_cartList.isEmpty) {
Fluttertoast.showToast(
msg: "Cart Empty, Add items to Cart",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
});
},
),
)
],
backgroundColor: Colors.redAccent[100],
),
drawer: MainDrawer(),
body: _buildGridView(),
);
}
GridView _buildGridView() {
return GridView.builder(
padding: const EdgeInsets.all(4.0),
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemCount: _dishes.length,
itemBuilder: (context, index) {
var item = _dishes[index];
return Card(
elevation: 4.0,
child: Stack(
fit: StackFit.loose,
alignment: Alignment.center,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(item.category),
Text(
item.brandName,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.subhead,
),
Text(
"R" + item.price.toString(),
textAlign: TextAlign.right,
)
],
),
Padding(
padding: const EdgeInsets.only(
right: 8.0,
bottom: 8.0,
),
child: Align(
alignment: Alignment.bottomRight,
child: GestureDetector(
child: (!_cartList.contains(item))
? Icon(
Icons.add_circle,
color: Colors.green,
)
: Icon(
Icons.remove_circle,
color: Colors.red,
),
onTap: () {
setState(() {
if (!_cartList.contains(item))
_cartList.add(item);
else
_cartList.remove(item);
});
},
),
),
),
],
));
});
}
void _populateDishes() {
var list = <Drinks>[
Drinks(
category: 'Gin Cocktails',
brandName: "Clover Club",
price: 65,
),
Drinks(
category: 'Gin Cocktail',
brandName: 'Tanqueray Sling',
price: 99,
),
Drinks(
category: 'Gin Cocktail',
brandName: 'French 75',
price: 84,
),
Drinks(
category: 'Gin Cocktail',
brandName: '1934 Cosmo',
price: 85,
),
Drinks(
category: 'Gin Cocktail',
brandName: "Sevilla Roseate",
price: 80,
),
Drinks(
category: 'Gin Cocktail',
brandName: 'TWR+ Sevilla Gimlet',
price: 75,
),
Drinks(
category: 'Gin Cocktail',
brandName: 'Watermelon Aperol Spiritz ',
price: 97,
)
];
setState(() {
_dishes = list;
});
}
}
The Drawer class
import 'package:flutter/material.dart';
import 'package:pass_data/cart.dart';
import 'package:pass_data/ginCocktails.dart';
import 'package:pass_data/whiskeyCocktails.dart';
import 'dish_object.dart';
import 'main.dart';
class MainDrawer extends StatelessWidget {
List<Drinks> _cartList = List<Drinks>();
#override
Widget build(BuildContext context) {
return Drawer(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
width: double.infinity,
padding: EdgeInsets.all(20),
color: Colors.redAccent[100],
child: Center(
child: Column(
children: <Widget>[
Container(
width: 100,
height: 100,
margin: EdgeInsets.only(top: 30, bottom: 20),
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage("lib/twr.png"),
fit: BoxFit.fill)),
),
Text("The Wing Republic",
style: TextStyle(fontSize: 22, color: Colors.white))
],
),
),
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"GIN COCKTAILS",
style: TextStyle(
fontSize: 18,
),
),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => ginCocktails()));
},
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"WHISK(E)Y COCKTAILS",
style: TextStyle(
fontSize: 18,
),
),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => whiskeyCocktails()));
},
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"COGNAC/BRANDY",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"RUM COCKTAILS",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"TEQUILLA COCKTAILS",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"Beers & Ciders",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"VODKA COCKTAILS",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"MOCKTAILS",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"Beers & Ciders",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"SHOOTERS",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"BEERS & CIDERS",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"VODKA",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"LIQUERS",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"WINE BY THE GLASS",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"WHITE WINE",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"RED WINE",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"MOSCATO | ROSE/BLUSH",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"BUBBLES",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"MCC",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"CHAMPAGNE",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"COGNAC",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"TEQUILLA",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
ListTile(
leading: Icon(Icons.local_drink),
title: Text(
"NON-ALCOHOLIC",
style: TextStyle(
fontSize: 18,
),
),
onTap: null,
),
],
),
));
}
}
Consider Using provider and even custom class of cart model for example:
CartModel{
final String name;
final int quantity;
final int price;
CartModel(this.name,this.quantity,this.price);
}
Cart can be now represented as List
Make sure to implement provider package that can be found here:
https://pub.dev/packages/provider
On this provided URL you find further information on how to use Provider package.
Now you have ability to manipulate your cart list with functions inside provider class.