10

The initial value in the dialog box doesn't change when I select an item. Here is the code for the dropdown list:

void _buildStatusDialog(String documentID) {
String _selectedText = "SDD";
showDialog<void>(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text("Status Update"),
        content: new DropdownButton<String>(
          hint: Text("Status"),
          value: _selectedText,
          items: <String>['SDD', 'Meeting', 'Home', 'Space']
              .map((String value) {
            return new DropdownMenuItem<String>(
              value: value,
              child: new Text(value),
            );
          }).toList(),
          onChanged: (String val) {
            _selectedText = val;
            setState(() {
              _selectedText = val;
            });
          },
        ),
        actions: <Widget>[
          FlatButton(
            child: Text("UPDATE"),
            onPressed: () {
              .....
            },
          ),
        ],
      );
    });

}

How do I update the "hint" or display the selected item?

enter image description here

4
  • 3
    Your setState call is updating the widget that built the dialog, not the dialog. Note that showDialog doesn't get called again. You need to replace your dialog with your own StatefulWidget Commented Jul 24, 2018 at 3:04
  • Is it possible to achieve this without creating another StatefulWidget? Commented Jul 24, 2018 at 3:25
  • 1
    no, you need to create a new Element which setState can mark dirty. Otherwise there is no way to update the widget besides closing/opening the dialog Commented Jul 24, 2018 at 3:27
  • 1
    Okay. Thanks Jonah! Commented Jul 24, 2018 at 3:31

2 Answers 2

9

Using @Jonah Williams's tip in the comments I came up with the following working example to a similar problem that I had:

import 'package:flutter/material.dart';

class StatusDialog extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return StatusDialogState();
  }
}

class StatusDialogState extends State<StatusDialog> {
  String _selectedText = "SSD";

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text("Status Update"),
      content: new DropdownButton<String>(
        hint: Text("Status"),
        value: _selectedText,
        items: <String>['SDD', 'Meeting', 'Home', 'Space']
            .map((String value) {
          return new DropdownMenuItem<String>(
            value: value,
            child: new Text(value),
          );
        }).toList(),
        onChanged: (String val) {
          setState(() {
            _selectedText = val;
          });
        },
      ),
      actions: <Widget>[
        FlatButton(
          child: Text("UPDATE"),
          onPressed: () {
            //.....
          },
        ),
      ],
    );
  }
}

void _buildStatusDialog(String documentID) {
  showDialog<void>(
    context: context,
    builder: (BuildContext context) {
      return StatusDialog();
    }
  );
}

Then you just need to add some logic to obtain _selectedText from StatusDialog - probably using a callback.

Sign up to request clarification or add additional context in comments.

Comments

0

add this line isExpanded: true, it will expand the arrow to the right of the container so the code will be like this:

return DropdownMenuItem<String>(
  value: value,
  child: Text(value),
  isExpanded: true,
);

1 Comment

This answers another question, not the one asked here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.