gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:35:09 25 4
gpt4 key购买 nike

对于DropDown,当我选择任何纵向选项时,该值将显示在dropDown中。
单击垂直下拉菜单项后,如何有效更改显示内容。
从下面的图片中可以看到。在“品牌”下拉列表中,一旦我选择了一个项目,它的值(value)就会显示出来。但是,我想更改显示的值。
我该怎么做?谢谢。
enter image description here
enter image description here

最佳答案

编辑后,请注意提示属性和 this.hintValue
您需要在onChanged事件中设置State并将值关联到像这样从onchanged获取的新值

onChanged: (String newValue) {
setState(() {
this.hintValue = newValue;
});
},
而:
 return DropdownButton<String>(
value: dropdownValue,
hint: Text("${this.hintValue}"),
icon: Icon(Icons.arrow_downward),
iconSize: 24,
完整代码将如下所示:
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(),
);
}
}
引用来自: flutter docs

关于flutter - 单击下拉菜单项后如何显示自定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64295055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com