Flutter popup menu item onTap implementation. In this post, I will be explaining how to implement Flutter popup menu item onTap using an easy Flutter example for better understanding. I will be using step by step explanation so you can have a better idea of how to use Flutter popup menu item onTap. I will also explain the role and usage of onTap in Flutter popup menu.
What is Flutter Popup Menu Item OnTap?
Flutter popup menu item onTap, as the names suggests, it is used to define what actions will be performed when you tap on one of the Flutter popup menu items from the list of popup menu. For instance, there is one item with the logout icon, we can specify an action that when the user taps on it then he/she should be navigate to the login screen.
Let’s understand the purpose and functionality of Flutter popup menu item onTap using a proper Flutter code example.
Implementing Flutter Popup Menu Item OnTap(All Steps)
For demonstration, we will show a snack bar for each item, whenever the user taps on one of the item from the list of popup menu items then a snack bar will be shown having the title text of that menu item.
For that we have to define a simple Flutter popup menu button, we have used one Flutter popup menu item to avoid the bulkiness of code. We have implemented the Flutter popup menu button in Flutter appbar widget using the appbar action constructor which takes a list of widgets only.
Using the value constructor of the popup menu item class, we can specify a value of type string, the reason is that the value constructor only takes string. This value will be used when the user taps on each option. If your popup menu has multiple items then each item has its value constructor, pass different values for each item so you can use it easily. Don’t worry, I will provide you with a complete source code in which you will have a better understanding of how to give different values to each item.
Now to implement the onTap functionality, we have to use the on selected constructor of the Flutter popup menu button widget class. It takes a function having a parameter of type object. We have used Flutter snackbar in the body of this function and passing the value to that snack bar so any option that is tapped, a snack bar will appear having that value. See the below code:
onSelected: (value) { ScaffoldMessenger.of(context) .showSnackBar(SnackBar(content: Text('$value item pressed'))); }
You can see in the first image that we have a popup menu item and as soon as we tap on it then a Flutter snackbar appears at the bottom of the screen as shown in the second image.
So this is how Flutter popup menu item onTap is implemented. Now you have a complete practical understanding of how to use and implement Flutter popup menu item onTap in your Flutter app. You can define other functions like when the user taps on it, it should be navigated to other screen and by using the value which should be unique for every item, you can specify an if else condition that if the value is of home option then navigate to home screen etc.
If you still have any queries regarding Flutter popup menu item onTap implementation then let me know in the comment section. I would love to answer all your queries.
As a treat, I have prepared a beautiful custom Flutter popup menu button design for you in which Flutter popup menu item onTap is also implemented. The complete source code is also available in the below block.
Custom Flutter Popup Menu Button Design Source Code
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Homepage(), ); } } class Homepage extends StatefulWidget { @override State<Homepage> createState() => _HomepageState(); } class _HomepageState extends State<Homepage> { @override Widget build(BuildContext context) { return SafeArea( child: Scaffold( appBar: AppBar( toolbarHeight: 80, elevation: 0, backgroundColor: Colors.transparent, actions: [ PopupMenuButton( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(20) .copyWith(topRight: Radius.circular(0))), padding: EdgeInsets.all(10), elevation: 10, color: Colors.grey.shade100, child: Container( alignment: Alignment.center, height: 45, width: 45, margin: EdgeInsets.all(20), decoration: BoxDecoration( boxShadow: [BoxShadow(blurRadius: 4, color: Colors.blue)], color: Colors.white, shape: BoxShape.circle), child: Icon( Icons.more_vert, color: Colors.blue, ), ), onSelected: (value) { ScaffoldMessenger.of(context) .showSnackBar(SnackBar(content: Text('$value item pressed'))); }, itemBuilder: (context) { return [ PopupMenuItem( value: 'Home', child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Icon( Icons.home, size: 20, color: Colors.blue, ), SizedBox( width: 5, ), Text( 'Home', style: TextStyle( color: Colors.blue, fontSize: 13, fontWeight: FontWeight.w500), ), ], ), Divider() ], )), PopupMenuItem( value: 'Chats', child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Icon( Icons.chat, size: 20, color: Colors.blue, ), SizedBox( width: 5, ), Text( 'Chats', style: TextStyle( color: Colors.blue, fontSize: 13, fontWeight: FontWeight.w500), ), ], ), Divider() ], )), PopupMenuItem( value: 'Sign out', child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Icon( Icons.logout, size: 20, color: Colors.blue, ), SizedBox( width: 5, ), Text( 'Sign out', style: TextStyle( color: Colors.blue, fontSize: 13, fontWeight: FontWeight.w500), ), ], ), Divider() ], )), ]; }, ) ], ), )); } }
Conclusion
In conclusion, we have discussed and practically implemented Flutter popup menu item onTap with proper Flutter example code. Your feedback is important regarding this post and I would encourage you to visit my other articles on Flutter app development, Flutter widgets, amazing Flutter templates, Flutter animations and more. Thanks for reading.