如何在flutter中从DropdownButtonFormField获取多个值



从下拉菜单中选择一个项目,我想在flutter中的文本字段中显示它的多个值。例如,如果我选择学校,它将显示其名称、位置和联系号码。因此我需要从该项目中收集多个值。如何让dropDownMenuItem中的多个值显示多个值?例如,学校列表项如下:

"SchoolList": [
{
"name": "school1",
"location": "location1",
"contact_no": "address1"
},
{
"name": "school2",
"location": "location2",
"contact_no": "address2"
},
{
"name": "school3",
"location": "location3",
"contact_no": "address3"
},

],

首先我们需要将Item类型作为DropdownButtonFormField的参数进行传递。Items将是该类型的列表,它将返回该类型的dropDownMenuItem。然后,我们将从onChanged部分中的项分配值。


DropdownButtonHideUnderline(
child: DropdownButtonFormField<
SchoolList>(
validator: (value) => value == null
? 'field required'
: null,
value: selectedItem,
icon: const Icon(
Icons.arrow_drop_down,
color: Colors.grey,
),
iconSize: 24,
elevation: 16,
hint: Text(
'Select',
),
onChanged:
(SchoolList?
schoolList) {
setState(() {

selectedSchoolName =
schoolList!
.name;
selectedSchoolLocation  =
schoolList!
.location;
selectedSchoolContactNo                                            
=
schoolList!
.contact_no;
});
},
//
items: =
SchoolList
?.map((item) {
return DropdownMenuItem<
SchoolList>(
value: item,
child:
Text(item.name)),
);
}).toList(),
),
),

相关内容

  • 没有找到相关文章

最新更新