Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't show the dropdown selected value in Flutter

I am new in Flutter development and I'm trying to show the selected value of a dropdown but I can't get it to work.

You can select one child and it works well but the dropdown doesn't show it as the chosen one, it just keeps like if nothing was selected.

Here is my code:

import 'package:app2date/repository/repository.dart';
import 'package:app2date/model/FeedSource.dart';
import 'package:app2date/model/FeedCategory.dart';
import 'package:app2date/util/ui.dart';
import 'package:flutter/material.dart';

class ManageFeedSource extends StatefulWidget {
  ManageFeedSource({Key key, this.feedSource}) : super(key: key);

  final FeedSource feedSource;

  @override
  _ManageFeedSource createState() => new _ManageFeedSource();
}

class _ManageFeedSource extends State<ManageFeedSource> {
  final tfNameController = new TextEditingController();
  final tfUrlController = new TextEditingController();
  var editMode = false;
  FeedCategory _feedCategory;

  @override
  Widget build(BuildContext context) {
    FeedSource feedSource = widget.feedSource;
    if (feedSource != null) {
      tfNameController.text = feedSource.name;
      tfUrlController.text = feedSource.url;
      editMode = true;
    }
    return Scaffold(
      appBar: AppBar(
        title: Text('New Feed'),
      ),
      body: new FutureBuilder(
        future: Repository.get().getFeedCategories(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          List<FeedCategory> categoriesList = snapshot.data;
          if (categoriesList != null) {
            if (categoriesList.isNotEmpty) {
                _feedCategory = categoriesList[0];      // The dropdown doesn’t draw the element (the list has elements)
            }
            print("${_feedCategory}");
            return new Padding(
              padding: EdgeInsets.all(12.0),
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  new DropdownButton<FeedCategory>(
                    hint: Text("Select Category"),
                    items: categoriesList.map((FeedCategory category) {
                      return new DropdownMenuItem<FeedCategory>(
                        value: _feedCategory,
                        child: Text(_feedCategory.name),
                      );
                    }).toList(),
                    onChanged: (FeedCategory category) {
                      setState(() {
                        _feedCategory = category;      // Problem here too, the element doesn’t show in the dropdown as selected
                        print("Selected: ${_feedCategory.name} (${_feedCategory.id})");
                      });
                    },
                  ),
                ],
              ),
            ),
          ],
          ),
          );
          } else {
          return Container(
          decoration: new BoxDecoration(color: Colors.white),
          );
          }
        },
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }
}

I would really appreciate any help.

like image 433
Alberto Méndez Avatar asked Aug 27 '18 20:08

Alberto Méndez


People also ask

How do I display the selected value of dropdown list in Flutter?

Step 1: Add a variable called dropdownValue that holds the currently selected item. Step 2: Add the DropdownButton widget to your page. Step 3: Inside the DropdownButton, add the value parameter and assign the dropdownValue that we created in step 1.

Why dropdown is not working in Flutter?

If the onChanged callback is null or the list of items is null then the dropdown button will be disabled, i.e. its arrow will be displayed in grey and it will not respond to input. A disabled button will display the disabledHint widget if it is non-null.

How do I show a drop down list in Flutter?

We will use the Flutter DropDownButton widget to display a dropdown list in our application. So first let's see what is DropDownButton. DropDownButton: In Flutter, A DropDownButton is a material design button. The DropDownButton is a widget that we can use to select one unique value from a set of values.

How do you clear dropdown value in Flutter?

To clear selected items in the DropDownList from Code behind, we can use the ClearSelection method. This method clears all the selected values.


1 Answers

Check the DropdownButton class , there is a property named value, use your variable _feedCategory in that place, and on your DropdownMenuItem map instead of _feedCategory use category:

   new DropdownButton<FeedCategory>(
                      value: _feedCategory,
                      hint: Text("Select Category"),
                      items: categoriesList.map((FeedCategory category) {
                        return new DropdownMenuItem<FeedCategory>(
                          value: category,
                          child: Text(category.name),
                        );
                      }).toList(),
                      onChanged: (FeedCategory category) {
                        setState(() {
                          _feedCategory = category;      // Problem here too, the element doesn’t show in the dropdown as selected
                          print("Selected: ${_feedCategory.name} (${_feedCategory.id})");
                        });
                      },
                    ),
like image 103
diegoveloper Avatar answered Sep 30 '22 06:09

diegoveloper