dev-resources.site
for different kinds of informations.
Flutter PopupMenuButton
Published at
9/9/2024
Categories
flutter
dart
programming
coding
Author
Aadarsh Kunwar
In Flutter, PopupMenuButton is a widget that provides a menu for selecting among a set of options. When tapped, it displays a list of items in a pop-up that the user can choose from.
Here’s a basic example of how to use PopupMenuButton:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: PopupMenuExample(),
);
}
}
class PopupMenuExample extends StatefulWidget {
@override
_PopupMenuExampleState createState() => _PopupMenuExampleState();
}
class _PopupMenuExampleState extends State<PopupMenuExample> {
String _selectedOption = 'None';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Popup Menu Button Example'),
actions: [
PopupMenuButton<String>(
onSelected: (String result) {
setState(() {
_selectedOption = result;
});
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
const PopupMenuItem<String>(
value: 'Option 1',
child: Text('Option 1'),
),
const PopupMenuItem<String>(
value: 'Option 2',
child: Text('Option 2'),
),
const PopupMenuItem<String>(
value: 'Option 3',
child: Text('Option 3'),
),
],
),
],
),
body: Center(
child: Text('Selected option: $_selectedOption'),
),
);
}
}
Explanation:
- PopupMenuButton: Creates a button that shows a pop-up menu.
- onSelected: Callback that is invoked when an item from the menu is selected.
- PopupMenuItem: Each item in the pop-up menu with a value and child (usually a Text widget).
Articles
12 articles in total
What's new in Flutter 3.27
read article
Top 10 flutter interview questions
read article
Beautiful Animations with CustomPaint in Flutter
read article
Slider in Flutter
read article
Back after a gap because of my exams
read article
Flutter Date and Time Picker
read article
Flutter PopupMenuButton
currently reading
Flutter DropdownButton
read article
Flutter ListTile
read article
Aadarsh Kunwar
read article
Flutter BottomNavigationBar
read article
Flutter
read article
Featured ones: