flutter - 如何在下拉颤动中设置初始默认值
问题描述
我有一个下拉菜单,下拉按钮中有一些值,默认情况下我需要选择值。您可以看到下面的片段,您可以在其中找到下拉值。我需要它,默认情况下总是选择正常值。希望你理解这个问题。
FormBuilder(
autovalidate: autovalidate,
child: FormBuilderCustomField(
attribute: "Select Address",
validators: [
FormBuilderValidators.required(),
],
formField: FormField(
builder: (FormFieldState<dynamic> field) {
return InputDecorator(
decoration: InputDecoration(
errorText: field.errorText,
filled: false,
isDense: true,
border: InputBorder.none,
icon: Container(
width: 50.0,
height: 50.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color: colorStyles['primary_light'],
),
child: Icon(
Icons.business_center,
color: colorStyles['primary'],
),
),
),
isEmpty: _typeValue == [],
child: new DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text("Service Type"),
isExpanded: true,
items: [
"normal",
"urgent",
"emergency",
].map((option) {
return DropdownMenuItem(
child: Text("$option"),
value: option,
);
}).toList(),
value: field.value,
onChanged: (value) {
field.didChange(value);
_serviceType = value;
},
),
),
);
},
)),
);
解决方案
只需在 initState() 上分配
selectedDropDownValue = "normal";
在 DropDown 中,将 selectedDropDownValue 分配给 value,并更新 onChanged 回调
new DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text("Service Type"),
isExpanded: true,
items: [
"normal",
"urgent",
"emergency",
].map((option) {
return DropdownMenuItem(
child: Text("$option"),
value: option,
);
}).toList(),
value: selectedDropDownValue, //asign the selected value
onChanged: (value) {
setState((){
selectedDropDownValue = value; //on selection, selectedDropDownValue i sUpdated
});
},
),
),
);
推荐阅读
- ios - 使用没有内容的 URL 调用 QLPreviewControllerDelegate 的方法 didSaveEditedCopyOf
- conda - 存储在 .conda 文件夹中的默认 Conda 环境
- indexing - 根据其他列中定义的间隔索引 pyspark 列
- android - Android 如何使用 XML 属性更改工具栏副标题的大小
- verilog - 这些行在 Verilog 中是否合法?
- node.js - 无效登录时的快速消息
- saml-2.0 - 作为 SAML 断言的一部分,如何以 userprncipalname 的身份发送电子邮件
- c - 为什么在链表从内存中释放后,下面的 C 代码不返回 0?
- python - 循环和停止复制输出 | 硒 | Python
- javascript - axios 连接超时