Search code examples
flutterdartdropdownflutter-layout

Remove Empty Space between Text and Trailer Icon of DropdownButton


I have a dropdown that display a list of strings. The values of the strings contain words that only consists of four letters and words that consists of many letters. This gives a layout problem when the selected item is the one with four letters. An empty or white space can be seen between the text and the trailing icon of the dropdown button. How can this be empty space be removed? How do I adjust the size of the dropdown button depending on the value selected?

Photo of Empty Space between text and trailing icon:

enter image description here

List:

List<String> textList = ["test", "this_is_a_long_text"];

Dropdown:

DropdownButtonHideUnderline(
      child: ButtonTheme(
       alignedDropdown: true,
       child: DropdownButton<String>(
        items: textList.map((String dropDownStringItem) {
           return DropdownMenuItem<String>(
                      value: dropDownStringItem,
                      child: Text(dropDownStringItem),
                    );
                  }).toList(),
              onChanged: (String newValueSelected) {
                _onDropDownItemSelected(newValueSelected);
              },
              value: _currentItemSelected,
            ),
          )),

Solution

  • as an option you can build it based on PopupMenuButton instead of regular DropdownButton

    below an example

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: Center(child: AwesomeDropdown()),
          ),
        );
      }
    }
    
    class AwesomeDropdown extends StatefulWidget {
      @override
      _AwesomeDropdownState createState() => _AwesomeDropdownState();
    }
    
    class _AwesomeDropdownState extends State<AwesomeDropdown> {
      final List<String> textList = ["test", "this_is_a111111_long_text"];
      String _currentItemSelected;
    
      @override
      void initState() {
        super.initState();
        _currentItemSelected = textList[0];
      }
    
      @override
      Widget build(BuildContext context) {
        return PopupMenuButton<String>(
          itemBuilder: (context) {
            return textList.map((str) {
              return PopupMenuItem(
                value: str,
                child: Text(str),
              );
            }).toList();
          },
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text(_currentItemSelected),
              Icon(Icons.arrow_drop_down),
            ],
          ),
          onSelected: (v) {
            setState(() {
              print('!!!===== $v');
              _currentItemSelected = v;
            });
          },
        );
      }
    }
    

    enter image description here