Open In App

Popping Button Class in Flutter Material Library with Example

A popping menu button is a widely used category of widgets that displays additional features to the user. They are generally used in the AppBar of the application for a quick menu option. The user is allowed to select from the provided options or navigate to a different screen based on his choice. To trigger the popup menu, a user has to press the three dots provided in the corner of the screen, and an onselect call back is invoked. They were given the name popup because of the popping functionality provided in Flutter. To use a popup menu button you need to import the material component package for flutter i.e. “package:flutter/material.dart”. 

Constructor

PopupMenuButton(
    {Key key, 
    @required PopupMenuItemBuilder<T> itemBuilder, 
    T initialValue, 
    PopupMenuItemSelected<T> onSelected, 
    PopupMenuCanceled onCanceled, 
    String tooltip, 
    double elevation, 
    EdgeInsetsGeometry padding: const EdgeInsets.all(8.0), 
    Widget child, 
    Widget icon, 
    Offset offset: Offset.zero, 
    bool enabled: true, 
    ShapeBorder shape, 
    Color color, 
    bool captureInheritedThemes: true}
) 

Parameter

itembuilder:  Popup menu comes with the main parameter. It is called when the button is pressed and creates items to be shown in the menu. 



PopupMenuButton(
                itemBuilder: (context) => [
                    PopupMenuItem(
                        //...
                    ),
                  ],
                ),

child: This is the button’s label

PopupMenuButton(
                child: CircleAvatar(
                    backgroundColor: Colors.yellow,
                    child: Text(
                      'Tap',
                    ),
                  ),
                  itemBuilder: (context) => [
                    PopupMenuItem(
                      //...
                    ),
                  ],
                ),

Properties

Methods

Adding Child to the Button

PopupMenuButton(
                child: CircleAvatar(
                    backgroundColor: Colors.yellow,
                    child: Text(
                      'Tap',
                    ),
                  ),
                   ),

Output:



 

Adding Menu Button Icon

PopupMenuButton(
                child: CircleAvatar(
                    backgroundColor: Colors.yellow,
                    child: Text(
                      'Tap',
                    ),
                  ),
                itemBuilder: (context) => 
                  
                ]
            )

Output:

 

Adding Menu to the Button

 PopupMenuButton(
                  child: CircleAvatar(
                    backgroundColor: Colors.yellow,
                    child: Text(
                      'Tap',
                    ),
                  ),
                  itemBuilder: (context) => [
                    PopupMenuItem(
                      child: Text("Profile"), // menu setting
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("Account"),
                      value: 2,
                    ),
                    PopupMenuItem(
                      child: Text("Settings"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("About GFG"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("Go Premium"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("Logout"),
                      value: 1,
                    ),
                  ],
                ),

Output:

 

Adjusting the Color for the Menu 

PopupMenuButton(
                  child: CircleAvatar(
                    backgroundColor: Colors.yellow,
                    child: Text(
                      'Tap',
                    ),
                  ),
                  
                  color: Colors.lightGreen, // color setting
                  
                  

                  itemBuilder: (context) => [
                    PopupMenuItem(
                      child: Text("Profile"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("Account"),
                      value: 2,
                    ),
                    PopupMenuItem(
                      child: Text("Settings"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("About GFG"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("Go Premium"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("Logout"),
                      value: 1,
                    ),
                  ],
                ),

Output:

 

Changing the Shape of the Menu 

 PopupMenuButton(
                  shape: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.black, width: 5)),
                  child: CircleAvatar(
                    backgroundColor: Colors.yellow,
                    child: Text(
                      'Tap',
                    ),
                  ),
                  color: Colors.lightGreen,
                  itemBuilder: (context) => [
                    PopupMenuItem(
                      child: Text("Profile"),
                      value: 1,
                    ),
                    PopupMenuItem( //...
                       ),
                    PopupMenuItem(//...
                     ),
                    PopupMenuItem(//...
                     ),
                    PopupMenuItem(//...
                    ),
                    PopupMenuItem(//...
                     ),
                  ],
                ),

Output:

 

Changing elevation 

 PopupMenuButton(
                   elevation: 50
                  shape: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.black, width: 5)),
                  child: CircleAvatar(
                    backgroundColor: Colors.yellow,
                    child: Text(
                      'Tap',
                    ),
                  ),
                  color: Colors.lightGreen,
                  itemBuilder: (context) => [
                    PopupMenuItem(
                      child: Text("Profile"),
                      value: 1,
                    ),
                    PopupMenuItem( //...
                       ),
                    PopupMenuItem(//...
                     ),
                    PopupMenuItem(//...
                     ),
                    PopupMenuItem(//...
                    ),
                    PopupMenuItem(//...
                     ),
                  ],
                ),

Output:

 

Let’s understand the above properties with the help of an example

Example




import 'package:flutter/material.dart';
  
void main() {
  runApp(HomeApp());
}
  
class HomeApp extends StatefulWidget {
  HomeApp({Key? key}) : super(key: key);
  
  @override
  State<HomeApp> createState() => _HomeAppState();
}
  
class _HomeAppState extends State<HomeApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
            appBar: AppBar(
              backgroundColor: Colors.green,
              title: const Text('GeeksforGeeks'),
              actions: [
                PopupMenuButton(
                  elevation: 50,
                  shape: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.black, width: 5)),
                  child: CircleAvatar(
                    backgroundColor: Colors.yellow,
                    child: Text(
                      'Tap',
                    ),
                  ),
                  color: Colors.lightGreen,
                  itemBuilder: (context) => [
                    PopupMenuItem(
                      child: Text("Profile"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("Account"),
                      value: 2,
                    ),
                    PopupMenuItem(
                      child: Text("Settings"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("About GFG"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("Go Premium"),
                      value: 1,
                    ),
                    PopupMenuItem(
                      child: Text("Logout"),
                      value: 1,
                    ),
                  ],
                ),
              ],
            ),
            body: const FirstScreen()));
  }
}
  
class FirstScreen extends StatelessWidget {
  const FirstScreen({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return Container(
        child: Center(
      child: RaisedButton(
        color: Colors.green,
        onPressed: () => Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => const NewScreen())),
        child: Text("GFG\'s PopUp Menu Tutorial"),
      ),
    ));
  }
}
  
class NewScreen extends StatefulWidget {
  const NewScreen({Key? key}) : super(key: key);
  
  @override
  State<NewScreen> createState() => _NewScreenState();
}
  
class _NewScreenState extends State<NewScreen> {
  TextEditingController textEditingController = TextEditingController();
  
  @override
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.green,
        title: const Text('New Screen'),
      ),
      body: Center(child: Text('This is your new screen')),
    );
  }
}

Output:


Article Tags :