0

从下拉菜单中选择一项,我想在颤动的文本字段中显示它的多个值。例如,如果我选择学校,它将显示其名称、位置、联系电话。所以我需要从那个项目中收集多个值。如何从 dropDownMenuItem 获得多个值来显示多个值?例如学校列表项目如下:

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

1 回答 1

0

首先,我们需要将 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(),
                                        ),
                                      ),
于 2021-12-19T05:55:32.040 回答