Is there a way to add a label to a DropdownButton in flutter?

I'm trying to get this kind of design: 1
[Label] : [Dropdown List]
[Label] : [Text field]
[Label] : [Dropdown List]

I'm already able to implement the dropdown using this code:

  List<String> _locations = ['Cita', 'Junta', 'Proyecto', 'Examen', 'Otro']; // Option 2
  String _selectedLocation; // Option 2

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Crear Evento'),
      ),
      body: Center(
        child: Container(
          width: double.infinity,
          height: 400,
          padding: EdgeInsets.symmetric(horizontal: 20),
          alignment: Alignment.center,
          child: Column(
            children: <Widget>[
              new Text('Categoría'),
              DropdownButton(
                hint: Text('Categoría'), // Not necessary for Option 1
                value: _selectedLocation,
                onChanged: (newValue) {
                  setState(() {
                    _selectedLocation = newValue;
                  });
                },
                items: _locations.map((location) {
                  return DropdownMenuItem(
                    child: new Text(location),
                    value: location,
                  );
                }).toList(),
              ),
            ],
          ),
        ),
      ),
    );

However, the output of this is not in the way I'd like, as the output is in the format: 2
[label]

PS.: I know it's not properly a label since it only is a text, if anyone could help me that'd be great, thanks in advance.

Upvotes: 3

Views: 6842

Answers (2)

Achintha Isuru
Achintha Isuru

Reputation: 3307

I know this is not the answer for the question, but anyone (who was like me, earlier) who wants to add a labelText like in FormFields, you can use the InputDecorator.

class ChecklistAddNewRepatDropdown extends StatelessWidget {
  final Map<int, List> tasks;
  final ChecklistAddNewState state;
  final int index;

  const ChecklistAddNewRepatDropdown({
     Key key,
     @required this.tasks,
     @required this.index,
     @required this.state,
   }) : super(key: key);

  @override
  Widget build(BuildContext context) {
 
 return InputDecorator(
  decoration: (state.emptyTasks.indexOf(index) == -1)
      ? inputDecoration('Repeat')
      : inputErrorDecoration('Repeat'),
  child: DropdownButtonHideUnderline(
    child: DropdownButton<String>(
      
      value: (tasks[index] == null || tasks[index][1] == "")
          ? 'One'
          : tasks[index][1],

      isExpanded: true,
      
      isDense: true,
      
      style: inputTextStyle(),
      
      icon: Icon(
        Icons.arrow_drop_down,
        color: Colors.black,
      ),
      
      iconSize: 24,

      onChanged: (value) {
        if (tasks[index] != null) {
          tasks[index][1] = value;
        } else {
          tasks[index] = ["", value];
        }
        checklistAddNewBloc.add(
          TasksTaskWhileChangingEvent(tasks),
        );
      },

      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    ),
  ),
);

you can sort of use it this way. (There may be errors in the code, I just want to give an example on how to use the InputDecorator).

Upvotes: 3

Fernando Rocha
Fernando Rocha

Reputation: 2599

If I got what you wanted right, you will need to use a Row (read more here) to do that, like the below example, using your code:

List<String> _locations = ['Cita', 'Junta', 'Proyecto', 'Examen', 'Otro']; // Option 2
  String _selectedLocation; // Option 2

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Crear Evento'),
      ),
      body: Center(
        child: Container(
          width: double.infinity,
          height: 400,
          padding: EdgeInsets.symmetric(horizontal: 20),
          alignment: Alignment.center,
          child: Column(
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('Categoría'),
                  Container(width: 8),
                  DropdownButton(
                    hint: Text('Categoría'), // Not necessary for Option 1
                    value: _selectedLocation,
                    onChanged: (newValue) {
                      setState(() {
                        _selectedLocation = newValue;
                      });
                    },
                    items: _locations.map((location) {
                      return DropdownMenuItem(
                        child: new Text(location),
                        value: location,
                      );
                    }).toList(),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );

which produces this:

enter image description here

Upvotes: 3

Related Questions