颤动:下拉列表中的选定值

时间:2018-07-24 03:01:33

标签: dart flutter dropdown

当我选择一个项目时,对话框中的初始值不会改变。这是下拉列表的代码:

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: () {
              .....
            },
          ),
        ],
      );
    });

}

如何更新“提示”或显示所选项目?

enter image description here

2 个答案:

答案 0 :(得分:2)

在评论中使用@Jonah Williams的技巧,我提出了以下工作示例来解决我遇到的类似问题:

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) {
          _selectedText = val;
          setState(() {
            _selectedText = val;
          });
        },
      ),
      actions: <Widget>[
        FlatButton(
          child: Text("UPDATE"),
          onPressed: () {
            //.....
          },
        ),
      ],
    );
  }
}

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

然后,您只需要添加一些逻辑即可从_selectedText获得StatusDialog-可能使用回调。

答案 1 :(得分:0)

添加此行user,它将展开容器右侧的箭头,因此代码将如下所示:

isExpanded: true