salim-lachdhaf / searchable_dropdown

Simple and robust Dropdown with item search feature, making it possible to use an offline item list or filtering URL for easy customization.
MIT License
326 stars 316 forks source link

Dropdown width fit to content #612

Open negulici-r-barnabas opened 7 months ago

negulici-r-barnabas commented 7 months ago

Is there a way to increase the width of the dropdown? I noticed that I can set the height via the PopupProps.menu using fit.loose but I am having difficulty with the width. My main issue is that the items are being wrapped to two lines if one of them is longer, I need them to fit but I don't know how to make the width of the dropdown (only the dropdown) larger. Does someone know how can I make the dropdown width larger to fit the content within? I also tried to use FittedBox in itemBuilder which works but the issue there is makes the text font size smaller (scales down the item to fit). image

knpaing commented 7 months ago

I think you can override the constraints of the PopupProps.menu property.

dhanesh12twice commented 4 months ago

constraints will work only when PositionCallback in menuProps in not null.

Future _openMenu() {
    // Here we get the render object of our physical button, later to get its size & position
    final popupButtonObject = context.findRenderObject() as RenderBox;
    // Get the render object of the overlay used in `Navigator` / `MaterialApp`, i.e. screen size reference
    var overlay = Overlay.of(context).context.findRenderObject() as RenderBox;
    return showCustomMenu<T>(
      menuModeProps: widget.popupProps.menuProps,
      context: context,
      position: (widget.popupProps.menuProps.positionCallback ?? _position)(
        popupButtonObject,
        overlay,
      ),
      child: _popupWidgetInstance(),
    );
}
RelativeRect _position(RenderBox popupButtonObject, RenderBox overlay) {
    // Calculate the show-up area for the dropdown using button's size & position based on the `overlay` used as the coordinate space.
    return RelativeRect.fromSize(
      Rect.fromPoints(
        popupButtonObject.localToGlobal(popupButtonObject.size.bottomLeft(Offset.zero), ancestor: overlay),
        popupButtonObject.localToGlobal(popupButtonObject.size.bottomRight(Offset.zero), ancestor: overlay),
      ),
      Size(overlay.size.width, overlay.size.height),
    );
}