Victor
Victor

Reputation: 157

Scrollable flutter popup menu

I'm trying to use flutter popup menu button, but I can't seem to make it smaller with a scroll.

Is it doable? Or am I using the wrong widget to do it?

Image below as reference, would like to show only the first 4 / 5 items, and scroll to show the rest!

Thanks in advance!

enter image description here

enter image description here

Upvotes: 3

Views: 5219

Answers (3)

Paul Inventador
Paul Inventador

Reputation: 21

You can use maxHeight for constrains property.

...
PopupMenuButton(
              constraints:
                  BoxConstraints(minWidth: context.maxWidth, maxHeight: 300),
...

Upvotes: 2

VICTOR MARTINS
VICTOR MARTINS

Reputation: 21

You can create this in two ways: the first one is PopupMenuButton widget and the second one is PopupRoute.

render

class HomePage extends StatefulWidget {

@override _HomepageState createState() => _HomepageState(); }

class _HomepageState extends State {

Listitems = [1,2,3,4,5,6,7,8,9,10,11,12,13];

@override Widget build(BuildContext context) {

return Scaffold(body: Center(
  child: PopupMenuButton(
      child: Icon(Icons.add_shopping_cart),
      offset: Offset(-1.0, -220.0),
      elevation: 0,
      color: Colors.transparent,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10))),
      itemBuilder: (context) {
        return <PopupMenuEntry<Widget>>[
          PopupMenuItem<Widget>(
            child: Container(
              decoration: ShapeDecoration(
                  color: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10))),
              child: Scrollbar(
                child: ListView.builder(
                  padding: EdgeInsets.only(top: 20),

                  itemCount: items.length,
                  itemBuilder: (context, index) {
                    final trans = items[index];
                    return ListTile(

                      title: Text(
                        trans.toString(),
                        style: TextStyle(
                          fontSize: 16,

                        ),
                      ),

                      onTap: () {
                        //what would you like to do?
                      },
                    );
                  },
                ),
              ),
              height: 250,
              width: 500,
            ),
          )
        ];
      }),
)

You can also adjust the number of items you want to show by reducing or increasing height of the container. I also added a scrollbar just in case.

Upvotes: 2

haroldolivieri
haroldolivieri

Reputation: 2283

You can create your own PopUp Widget instead.

A Card wrapped into a AnimatedContainer with specific dimensions and a ListView inside.

Place this widget on your screen using Stack and Positioned widgets so it will be above other elements on the top | right.

class CustomPopup extends StatefulWidget {
  CustomPopup({
    @required this.show,
    @required this.items,
    @required this.builderFunction,
  });

  final bool show;
  final List<dynamic> items;
  final Function(BuildContext context, dynamic item) builderFunction;

  @override
  _CustomPopupState createState() => _CustomPopupState();
}

class _CustomPopupState extends State<CustomPopup> {
  @override
  Widget build(BuildContext context) {
    return Offstage(
      offstage: !widget.show,
      child: AnimatedContainer(
        duration: Duration(milliseconds: 300),
        height: widget.show ? MediaQuery.of(context).size.height / 3 : 0,
        width: MediaQuery.of(context).size.width / 3,
        child: Card(
          elevation: 3,
          child: MediaQuery.removePadding(
            context: context,
            removeTop: true,
            child: ListView.builder(
              scrollDirection: Axis.vertical,
              itemCount: widget.items.length,
              itemBuilder: (context, index) {
                Widget item = widget.builderFunction(
                  context,
                  widget.items[index],
                );
                return item;
              },
            ),
          ),
        ),
      ),
    );
  }
}
return Stack(
      children: <Widget>[
        Container(
          color: Colors.blueAccent,
        ),
        Positioned(
          right: 0, 
          top: 60, 
          child: CustomPopup(
                  show: shouldShow,
                  items: [1, 2, 3, 4, 5, 6, 7, 8],
                  builderFunction: (context, item) {
                    return ListTile(
                       title: Text(item.toString()),
                       onTap: () {}
                    );
                  },
              ),
          ),
      ],
    );

Upvotes: 2

Related Questions