Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Floating Action Button with a popup menu in flutter?

I need to make a popup menu kind of button. Is there any way to make a pop up menu floating action button,this is my desired view

like image 295
Ashok Rout Avatar asked Jul 20 '20 14:07

Ashok Rout


People also ask

How do I show the popup menu in flutter?

Displays a menu when pressed and calls onSelected when the menu is dismissed because an item was selected. The value passed to onSelected is the value of the selected menu item. If we focus on an Application, We can see in every Application there is a Pop Up Menu button that will do some work.


Video Answer


2 Answers

You can use flutter speed dial package. Visit - https://pub.dev/packages/flutter_speed_dial . And here is a youtube video - https://www.youtube.com/watch?v=1FmATI4rOBc

like image 187
Mausom Saikia Avatar answered Nov 10 '22 23:11

Mausom Saikia


Your answer is PopupMenuItem class, which will help you get the desirable result.

PLEASE NOTE: I have just demonstrated how to use, and with what code, you achieve the result. You can anyways, play with it, and get your desirable result.

CODE SNIPPET FOR CREATING A POPUP MENU ITEM

PopupMenuButton<Choice>(
    itemBuilder: (context) => [
        PopupMenuItem()
    ],
    icon: Icon(),
    offset: Offset()
)

CODE FOR REFERENCE

class _MyHomePageState extends State<MyHomePage> {
  
  Widget _offsetPopup() => PopupMenuButton<int>(
    itemBuilder: (context) => [
          PopupMenuItem(
            value: 1,
            child: Text(
              "Flutter Open",
              style: TextStyle(
                  color: Colors.black, fontWeight: FontWeight.w700),
            ),
          ),
          PopupMenuItem(
            value: 2,
            child: Text(
              "Flutter Tutorial",
              style: TextStyle(
                  color: Colors.black, fontWeight: FontWeight.w700),
            ),
          ),
        ],
    icon: Container(
      height: double.infinity,
      width: double.infinity,
      decoration: ShapeDecoration(
        color: Colors.blue,
        shape: StadiumBorder(
          side: BorderSide(color: Colors.white, width: 2),
        )
      ),
      //child: Icon(Icons.menu, color: Colors.white), <-- You can give your icon here
    )
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        padding: EdgeInsets.only(right: 10.0, bottom: 10.0),
        child: Align(
          alignment: Alignment.bottomRight,
          child: Container(
            height: 80.0,
            width: 80.0,
            child: _offsetPopup()
          )
        )
      )
    );
  }
}

The above will give you this result:

RESULTANT GIF

PRO-TIP

You can play around with Offset() to decide the position of your PopupMenuItems

like image 22
Alok Avatar answered Nov 10 '22 23:11

Alok