The PopupMenuButton widget creates a button that, when pressed, displays a menu of selectable items. Users can select items from the menu. Items in a menu are typically represented by instances of PopupMenuItem.
There Are Some Common Attributes for PopupMenuButton
onSelected | Your menu item selection logic goes here |
PopupMenuItem | Set Menu Name |
Here's a simple example of using a PopupMenuButton in Flutter:
File open lib->main.dart file −
import 'package:flutter/material.dart';
void main() {
runApp(
MyApp()
);
}
class MyApp extends StatefulWidget{
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('PopupMenuItem Example',
style:TextStyle(fontSize: 20,color: Colors.white)),
actions: [
PopupMenuButton<String>(
onSelected: (String result) {
// Your menu item selection logic goes here
print('Selected: $result');
},
itemBuilder: (BuildContext context) => [
PopupMenuItem<String>(
value: 'option1',
child: Text('Option 1'),
),
PopupMenuItem<String>(
value: 'option2',
child: Text('Option 2'),
),
PopupMenuItem<String>(
value: 'option3',
child: Text('Option 3'),
),
],
),
],
),
body: Center(
child:
Text("PopupMenuItem Example"),
)
));
}
}
Open
Device Manager, run the emulator, and then run the application. Next,
check the working output and check the output you declared in your
code.
Output: