how to reset DropdownSearch.multiSelection() based on other dropdown in flutter?

Viewed 45

how to reset DropdownSearch.multiSelection() based on other dropdown in flutter?

in my app, i have to reset DropdownSearch.multiSelection() based on one another dropdown.

Here is my Code for DropdownSearch.multiSelection().

 DropdownSearch<ComplaintBook>.multiSelection(
                        items: dropDownComplaintBookModels,
                        popupProps: PopupPropsMultiSelection.modalBottomSheet(
                            showSearchBox: true,
                            searchFieldProps: TextFieldProps(
                              decoration: InputDecoration(
                                labelText: "Complaints*",
                                prefixIcon: Icon(Icons.restart_alt_outlined),
                              ),
                            )),
                        dropdownDecoratorProps: DropDownDecoratorProps(
                          dropdownSearchDecoration: InputDecoration(
                            border: OutlineInputBorder(),
                            prefixIcon: Icon(Icons.restart_alt_outlined),
                            labelText: "Complaints*",
                            // hintText: "Select an Int",
                          ),
                        ),
                        itemAsString: (ComplaintBook complaint) =>
                            complaint.tokenNumber!,
                        onChanged: (List<ComplaintBook>? complaint) {
                          selectedComplaintBooks = complaint!;
                        },
                        selectedItems: selectedComplaintBooks,
                        validator: (value) {
                          if (value!.isEmpty) {
                            return "Select atleast one Complaint !";
                          }
                          return null;
                        },
                      ),

and i have to reset above dropdown based on below dropdown.

DropdownSearch<Customer>(
                        items: dropDownCustomerModels,
                        popupProps: PopupProps.modalBottomSheet(
                            showSearchBox: true,
                            searchFieldProps: TextFieldProps(
                              decoration: InputDecoration(
                                labelText: "Customer*",
                                prefixIcon: Icon(Icons.person),
                              ),
                            )),
                        dropdownDecoratorProps: DropDownDecoratorProps(
                          dropdownSearchDecoration: InputDecoration(
                            border: OutlineInputBorder(),
                            prefixIcon: Icon(Icons.person),
                            labelText:
                                AppLocalizations.of(context).lblCustomer + "*",
                            // hintText: "Select an Int",
                          ),
                        ),
                        itemAsString: (Customer cust) => cust.companyName!,
                        onChanged: (Customer? customer) {
                          setState(() {
                            StateModel state = customer!.state!;
                            stateController.text = state.name ?? "-";
                            selectedComplaintBooks.clear();
                            setState(() {});
                            presenter.getComplaintBookList(customer.id!);
                            dropdownValueCustomer = customer;
                            print(customer.companyName);
                          });
                        },
                        validator: (value) {
                          if (value == null) {
                            return "Select Customer !";
                          }
                          return null;
                        },
                      ),

as code i clear the selectedComplaintBooks list but it still shows selected item in dropdownsearch.multiselaction as below image. enter image description here

so how I can reset the DropdownSearch.multiSelection() based on DropdownSearch()?

Please help me. Thank You.

1 Answers

I Solved this problem using GlobalKey.

I just give a global key to DropdownSearch.multiSelection()

as below.

final dropDownKey = GlobalKey<DropdownSearchState<ComplaintBook>>();
 DropdownSearch<ComplaintBook>.multiSelection(
                        key: dropDownKey,
)

and reset it in DropdownSearch() onchnage Like.

onChanged: (Customer? customer) {
                          setState(() {
                            selectedComplaintBooks = [];
                            dropDownKey.currentState!.clear();
                          });
                        },

and its Done.

Related