Flutter / UI Elements / PopupMenuButton
PopupMenuButton
-
Usage
The PopupMenuButton widget in Flutter creates a button that, when pressed, shows a menu of items to choose from. It's commonly used to provide a menu of options or actions for a particular widget or screen.
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } enum MenuOptions { option1, option2, option3 } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('PopupMenuButton Example'), actions: [ PopupMenuButton ( onSelected: (MenuOptions result) { // Handle selection from the menu switch (result) { case MenuOptions.option1: // Handle option 1 print('Option 1 selected'); break; case MenuOptions.option2: // Handle option 2 print('Option 2 selected'); break; case MenuOptions.option3: // Handle option 3 print('Option 3 selected'); break; } }, itemBuilder: (BuildContext context) => >[ const PopupMenuItem ( value: MenuOptions.option1, child: Text('Option 1'), ), const PopupMenuItem ( value: MenuOptions.option2, child: Text('Option 2'), ), const PopupMenuItem ( value: MenuOptions.option3, child: Text('Option 3'), ), ], ), ], ), body: Center( child: Text('Tap the icon in the AppBar to see options'), ), ), ); } } In this example, a PopupMenuButton is added to the AppBar of the Scaffold. The PopupMenuButton has an itemBuilder property, where you can define the list of menu items (PopupMenuItem). The onSelected callback is triggered when the user selects an item from the menu, and you can handle the selected option accordingly.