简体   繁体   English

如何从 flutter 中的 DropdownButtonFormField 获取多个值?

[英]How to get multiple values from DropdownButtonFormField in flutter?

selecting one item from dropdown menu, i want to show its multiple values inside textfields in flutter.从下拉菜单中选择一项,我想在 flutter 的文本字段中显示其多个值。 For example, if I select school it will show its name, location, contact no.例如,如果我是 select 学校,它将显示其名称、位置、联系电话。 so i need to collect multiple values from that item.所以我需要从那个项目中收集多个值。 how can i have multiple values from dropDownMenuItem to show multiple values?如何从 dropDownMenuItem 获得多个值来显示多个值? for example School list item is given below:例如学校列表项目如下:

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

at first we need to pass Item type as parameter of DropdownButtonFormField.首先,我们需要将 Item 类型作为 DropdownButtonFormField 的参数传递。 Items will me that type of list and it will return dropDownMenuItem of that type. Items 将我该类型的列表,它将返回该类型的 dropDownMenuItem。 Then we will assign values from items inside onChanged section.然后我们将从 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(),
                                        ),
                                      ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM