如何在Flutter中从DropdownButtonFormField中获取多个值?

huwehgph  于 2023-03-31  发布在  Flutter
关注(0)|答案(1)|浏览(220)

从下拉菜单中选择一个项目,我想在flutter中显示其文本字段内的多个值.例如,**如果我选择学校,它将显示其名称,位置,联系方式.**所以我需要从该项目中收集多个值.我如何从dropDownMenuItem中有多个值来显示多个值?例如学校列表项如下所示:

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

bxjv4tth1#

首先,我们需要传递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(),
                                        ),
                                      ),

相关问题