Flutter DropdownButton show label when option is selected

Issue

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

Solution

Replace DropdownButton with DropdownButtonFormField:

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

Answered By – dfmiller

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.