简体   繁体   中英

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. For example, if I select school it will show its name, location, contact no. so i need to collect multiple values from that item. how can i have multiple values from dropDownMenuItem to show multiple values? 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. Items will me that type of list and it will return dropDownMenuItem of that type. Then we will assign values from items inside onChanged section.


     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(),
                                        ),
                                      ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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