Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollable flutter popup menu

Tags:

flutter

dart

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

like image 539
Victor Avatar asked Jun 30 '26 09:06

Victor


1 Answers

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: () {}
                    );
                  },
              ),
          ),
      ],
    );
like image 95
haroldolivieri Avatar answered Jul 03 '26 00:07

haroldolivieri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!