简体   繁体   English

单击下拉菜单项后如何显示自定义值

[英]How to display a custom value after clicking A Drop Down Menu Item

For a DropDown when I select any purticular option that value gets displayed in the dropDown.对于 DropDown,当我 select 任何 purticular 选项时,该值显示在 dropDown 中。

How do I effectively change what is displayed once a purticular drop down menu item is clicked on.单击特定下拉菜单项后,如何有效更改显示的内容。

As you can see from the below images.正如您从下面的图片中看到的那样。 In the Brands Dropdown once I select an item its value gets displayed.在品牌下拉列表中,一旦我 select 一个项目,它的值就会显示出来。 However, I would like to change the value that is displayed.但是,我想更改显示的值。

How do I accomplish that?我如何做到这一点? Thanks.谢谢。

在此处输入图像描述 在此处输入图像描述

EDITED please pay attention on hint property and this.hintValue编辑请注意提示属性和this.hintValue

You need to set State in onChanged event and associate value to new value grabbed from onchanged like this您需要在 onChanged 事件中设置 State 并将值关联到像这样从 onchanged 获取的新值

onChanged: (String newValue) {
    setState(() {
      this.hintValue = newValue;
    });
  },

while:尽管:

 return DropdownButton<String>(
  value: dropdownValue,
  hint: Text("${this.hintValue}"),
  icon: Icon(Icons.arrow_downward),
  iconSize: 24,

fullcode will be like this:完整代码将是这样的:

class DropDownWidget extends StatefulWidget {

  DropDownWidget({Key key}) : super(key: key);

  @override
  _DropDownWidgetState createState() => _DropDownWidgetState();
}

/// This is the private State class that goes with MyStatefulWidget.
class _DropDownWidgetState extends State<DropDownWidget> {
  String dropdownValue = 'One';
  String hintValue;
  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      value: dropdownValue,
      hint: Text("${this.hintValue}"),
      icon: Icon(Icons.arrow_downward),
      iconSize: 24,
      elevation: 16,
      style: TextStyle(color: Colors.deepPurple),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String newValue) {
        setState(() {
          this.hintValue = newValue;
        });
      },
      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    );
  }
}

reference from: flutter docs参考自: flutter 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM