[英]Dropdown list with condition. Flutter/Dart
This is a dropdown list I have.这是我的下拉列表。 How do I change the code to have a condition where if the user chooses an option it will give "valueChoose_f4" that chosen option else give "valueChoose_f4" the value that is stored in "M${widget.orgUnit}".
如何更改代码以具有条件,如果用户选择一个选项,它将为“valueChoose_f4”提供选择的选项,否则为“valueChoose_f4”提供存储在“M${widget.orgUnit}”中的值。 Thanks, hope my question made sense.
谢谢,希望我的问题有意义。
ListTileTheme(
tileColor: Colors.white,
child: ListTile(
title: Container(
decoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(color: Colors.blueGrey),
borderRadius: BorderRadius.circular(10),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
isExpanded: true,
value: valueChoose_f4,
hint: Text('M${widget.orgUnit}'),
onChanged: (newValue){
setState(() {
valueChoose_f4 = newValue;
});
},
items: listUserType.map((map) {
return DropdownMenuItem<String>(
value: map['value'],
child: Text(map['name']),
);
}).toList(),
),
),
),
),
),
),
You can't give a dropdown value that is not contained in your listUserType so if you want to set a default value where the dropdown initializes and not has been selected yet you can do it like:您不能提供未包含在 listUserType 中的下拉列表值,因此,如果您想设置下拉列表初始化且尚未选择的默认值,您可以这样做:
value: valueChoose_f4 ?? 'Your_default_value'
that means when valueChoose_f4 is null, the default value will be instead the null.这意味着当 valueChoose_f4 为 null 时,默认值将改为 null。
String? valueChoose_f4;
ListTile(
title: Container(
decoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(color: Colors.blueGrey),
borderRadius: BorderRadius.circular(10),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
isExpanded: true,
value: valueChoose_f4 ?? listUserType.first['value'], // changed
hint: const Text('M${widget.orgUnit}'),
onChanged: (newValue){
setState(() {
valueChoose_f4 = newValue;
});
},
items: listUserType.map((map) {
return DropdownMenuItem<String>(
value: map['value'],
child: Text(map['name']),
);
}).toList(),
),
),
),
),
)
I hope this solve your problem.我希望这能解决你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.