Ciprian
Ciprian

Reputation: 3226

Flutter DropdownButton show label when option is selected

Can a Dropdown Button:

 return DropdownButton<String>(
          items: <String>['Foo', 'Bar'].map((String value) {
            return new DropdownMenuItem<String>(
              value: value,
              child: new Text(value),
            );
          }).toList(),
          onChanged: (_) {},
        );

have something similar to a decoration user in a TextFormField:

      TextFormField(
        controller: _titleController,
        decoration: InputDecoration(labelText: 'Input'),
        validator: (String value) {
          if (value != null && value.isEmpty) {
            return 'Please enter some text';
          }
        },
        style: Theme.of(context).textTheme.title,
      ),

When something is written in the above TextFormField the word Input shows. Like this:

enter image description here

Upvotes: 25

Views: 25774

Answers (3)

JayLord Abueva
JayLord Abueva

Reputation: 435

Change DropdownButton to DropdownButtonFormField and add this decoration....

DropdownButtonFormField<String>(
  decoration: InputDecoration(
    filled: true,
    fillColor: Hexcolor('#ecedec'),
    labelText: 'Occupation',
    border: new CustomBorderTextFieldSkin().getSkin(),
  ),
  //...
)

Upvotes: 28

Abdullah
Abdullah

Reputation: 567

Copy Paste and see magic
I have done flutter dropdown with material design

    Padding(
          padding: const EdgeInsets.all(9.0),
          child: InputDecorator(
            decoration: InputDecoration(
              labelText: 'Priority',
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(5.0)),
              contentPadding: EdgeInsets.all(10),
            ),
            child: ButtonTheme(
              materialTapTargetSize: MaterialTapTargetSize.padded,
              child: DropdownButton<String>(
                hint: const Text("Priority"),
                isExpanded: true,
                value: dropdownValue,
                elevation: 16,
                underline: DropdownButtonHideUnderline(
                  child: Container(),
                ),
                onChanged: (String? newValue) {
                  setState(() {
                    dropdownValue = newValue!;
                  });
                },
                items: <String>['One', 'Two', 'Free', 'Four']
                    .map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
              ),
            ),
          ),
        ),
    

Upvotes: 1

dfmiller
dfmiller

Reputation: 1203

Replace DropdownButton with DropdownButtonFormField:

https://api.flutter.dev/flutter/material/DropdownButtonFormField-class.html

Upvotes: 36

Related Questions