fluttermaterial-uimaterial-designflutter-ui

DropdownButtonFormField selected item being clipped off


Within a DropdownButtonFormField with the following selectedItemBuilder:

selectedItemBuilder: (context) {
    return widget.items!.map<Widget>((item) {
        return Padding(
            padding: const EdgeInsets.only(top: 12.0),
            child: Text(
                item.name!,
                style: const TextStyle(fontSize: Sizes.p12),
            ),
        );
    }).toList();
},

The selected items are clipped off. Tried contentPadding parameter inside decoration's InputDecoration.

Example clipping off


Solution

  • Having a Stack with clipBehavior set to Clip.none alleviates this issue. Also used Positioned instead of using Padding:

    selectedItemBuilder: (context) {
        return widget.items.map<Widget>((item) {
            return Stack(
                clipBehavior: Clip.none,
                children: [
                    Positioned(
                      bottom: -6,
                      child: Text(
                        item.name!,
                        style: const TextStyle(fontSize: Sizes.p12),
                      ),
                    )
                ],
            );
        }).toList();
    }
    

    Edit: Full code requested, it's as shown below:

    Container(
          height: 50.0,
          child: DropdownButtonFormField<Item>(
            value: widget.items,
            menuMaxHeight: 300.0,
            onChanged: (Item? item) {
              widget.updateCallback(item!);
            },
            isDense: true,
            isExpanded: true,
            selectedItemBuilder: (context) {
              return listOfValues!.map((item) {
                return Stack(
                  clipBehavior: Clip.none,
                  fit: StackFit.expand,
                  children: [
                    Positioned(
                      bottom: -6,
                      child: SizedBox(
                        width: 90.0,
                        child: FittedBox(
                          child: Text('${item.name!} - ${item.itemPercentage}%',
                          ),
                        ),
                      ),
                    )
                  ],
                );
              }).toList();
            },
            items: someList!
                .map((Choice choice) {
              return DropdownMenuItem<Item>(
                value: choice,
                child: Text(
                  '${choice.name!} - ${choice.percemtage}%',
                  overflow: TextOverflow.ellipsis,
                  maxLines: 2,
                ),
              );
            }).toList(),
          ),
        )
    

    Not sure if this runs as is as I had to strip some very specific preferences (colours, variable names etc.). But you get the general idea :).