Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop down button in flutter not switching values to the selected value

Tags:

I've recently started programming using dart and flutter and everything has been going smoothly for my app, although recently i wanted to add drop down menu to provide the user with multiple options to pick from. everything worked as planned however when i pick a value from the list it doesn't change the value in the box, it goes back to the hint or an empty box. any help would be appreciated!

here is my code for the dropdownbutton:

Widget buildDropdownButton() {
String newValue;

return new Padding(
  padding: const EdgeInsets.all(24.0),
  child: new Column(
    mainAxisAlignment: MainAxisAlignment.start,
    children: <Widget>[
      new ListTile(
        title: const Text('Frosting'),
        trailing: new DropdownButton<String>(
            hint: Text('Choose'),
            onChanged: (String changedValue) {
              newValue=changedValue;
              setState(() {
                newValue;
                print(newValue);
              });
            },
            value: newValue,
            items: <String>['None', 'Chocolate', 'Vanilla', 'ButterCream']
                .map((String value) {
              return new DropdownMenuItem<String>(
                value: value,
                child: new Text(value),
              );
            }).toList()),
      ),
    ],
  ),
);
}
like image 862
Darwish Al-Neyadi Avatar asked Aug 07 '18 21:08

Darwish Al-Neyadi


People also ask

How do you change the dropdown value in flutter?

To change the dropdown Button color in Flutter, simply wrap your DropdownButton widget inside the Container and provide the styling instructions inside the decoration property using the BoxDecoration widget. Using the color property (insideBoxDecoration) you can assign the color.

How do you show the selected value in a dropdown 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.


3 Answers

The error is because you are declaring a method variable newValue you must declare that variable as global inside your StatefulWidget.

   String newValue;

  Widget buildDropdownButton() {
  return new Padding(
    padding: const EdgeInsets.all(24.0),
    child: new Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        new ListTile(
          title: const Text('Frosting'),
          trailing: new DropdownButton<String>(
              hint: Text('Choose'),
              onChanged: (String changedValue) {
                newValue=changedValue;
                setState(() {
                  newValue;
                  print(newValue);
                });
              },
              value: newValue,
              items: <String>['None', 'Chocolate', 'Vanilla', 'ButterCream']
                  .map((String value) {
                return new DropdownMenuItem<String>(
                  value: value,
                  child: new Text(value),
                );
              }).toList()),
        ),
      ],
    ),
  );
  }
like image 81
diegoveloper Avatar answered Sep 18 '22 18:09

diegoveloper


Faced same issue and none of the answers worked. Then, I found the solution in one of my old projects.

I was using it in a AlertDialog here.

So, Change DropdownButton to DropdownButtonFormField

and add onSaved exactly as onChanged:

onSaved: (value) {
    setState(() {
        _selectedValue = value;
    });
}

That's it. It will start working.

like image 21
Lalit Fauzdar Avatar answered Sep 20 '22 18:09

Lalit Fauzdar


I had this problem although I was already using the solution above.

for anyone who has this problem and the above solution does not work, try separating FutureBuilder from the dropdown. this is how your final code should look like:

class TheFuture extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: myFuture(),
      builder: (ctx, snp) {
        if (!snp.hasData) return LoadingLine();
        return TheBuilder(snp.data);
      },
    );
  }
}

class TheBuilder extends StatefulWidget {
  const TheBuilder(this.mp);
  final Map<String, dynamic> mp;
  @override
  _MessageUSScreenFilterBodyState createState() =>
      _MessageUSScreenFilterBodyState();
}

class _MessageUSScreenFilterBodyState extends State<MessageUSScreenFilterBody> {
  int _selectedId;

  @override
  Widget build(BuildContext context) {
    return DropdownButton<int>(
      selectedItemBuilder: (context) =>
          widget.mp['myData'].map((e) => Text(e.type)).toList(),
      items: widget.mp['myData']
          .map(
            (e) => DropdownMenuItem(
              child: Text(e.type),
              value: e.id,
            ),
          )
          .toList(),
      value: _selectedId,
      onChanged: (int _id) {
        setState(() {
          _selectedId = _id;
        });
      },
    );
  }
}
like image 27
Adnan Avatar answered Sep 16 '22 18:09

Adnan