salim-lachdhaf / searchable_dropdown

Simple and robust Dropdown with item search feature, making it possible to use an offline item list or filtering URL for easy customization.
MIT License
326 stars 316 forks source link

when I select one item it selects all items #614

Closed mazab99 closed 4 months ago

mazab99 commented 6 months ago

DropdownSearch.multiSelection( asyncItems: (text) async { final Response response = await Dio().post( '${ApiConstants.brandsEndPoint}', data: { "search": text, 'category_id': widget.category_id, }, );

                                final List<BrandsModel> countries =
                                    BrandsModel.jsonToList(
                                  response.data['data'],
                                );
                                return countries;
                              },
                              itemAsString: (item) {
                                return item.name;
                              },
                              compareFn: (item1, item2) {
                                return true;
                              },
                              dropdownBuilder: (context, selectedItem) {
                                return Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Text(
                                    selectedItem
                                        .map((e) => e.name)
                                        .toString(),
                                  ),
                                );
                              },
                              onChanged: (value) {
                                setState(() {
                                  selectedValues = value.map((e) {
                                    return e.name;
                                  }).toList();
                                });
                              },
                              popupProps: PopupPropsMultiSelection.menu(
                                isFilterOnline: true,
                                showSearchBox: true,
                                showSelectedItems: true,
                                loadingBuilder: (context, searchEntry) {
                                  return MyProgressCircle();
                                },
                                itemBuilder: (context, item, isSelected) {
                                  return Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: Text(
                                      item.name,
                                      style: const TextStyle(
                                        color: Colors.black,
                                        fontSize: 20,
                                      ),
                                    ),
                                  );
                                },
                                searchFieldProps: TextFieldProps(
                                  decoration: InputDecoration(
                                    enabledBorder: OutlineInputBorder(
                                      borderSide: BorderSide(
                                          color: MyHexColor('#ECEFF5'),
                                          width: 1),
                                      borderRadius:
                                          BorderRadius.circular(15.r),
                                    ),
                                    focusedBorder: OutlineInputBorder(
                                      borderSide: BorderSide(
                                        width: 1,
                                        color: MyHexColor('#ECEFF5'),
                                      ),
                                      borderRadius:
                                          BorderRadius.circular(15.r),
                                    ),
                                    errorBorder: OutlineInputBorder(
                                      borderSide: BorderSide(
                                        width: 1,
                                        color: MyHexColor('#EB5757'),
                                      ),
                                      borderRadius:
                                          BorderRadius.circular(15.r),
                                    ),
                                    focusedErrorBorder: OutlineInputBorder(
                                      borderSide: BorderSide(
                                        width: 1,
                                        color: MyHexColor('#ECEFF5'),
                                      ),
                                      borderRadius:
                                          BorderRadius.circular(15.r),
                                    ),
                                    hintStyle: TextStyle(
                                      fontSize: 16.sp,
                                      color: MyHexColor('000000'),
                                      fontFamily: 'TajawalRegular400',
                                    ),
                                    filled: true,
                                    hintText: AppLocalizations.of(context)!
                                        .search,
                                    contentPadding: EdgeInsets.all(15),
                                  ),
                                ),
                                textDirection: APPLANGUAGE == Locale('ar')
                                    ? TextDirection.rtl
                                    : TextDirection.ltr,
                                scrollbarProps: ScrollbarProps(
                                  thumbColor: HexColorManager.mainAppColor,
                                ),
                                menuProps: MenuProps(
                                  shape: RoundedRectangleBorder(
                                    side: BorderSide(
                                      color: HexColorManager.mainAppColor,
                                    ),
                                  ),
                                ),
                                // validationWidgetBuilder: (context, item) {
                                //   return MaterialButton(
                                //     onPressed: () {},
                                //     color: HexColorManager.mainAppColor,
                                //     child: Text(
                                //       AppLocalizations.of(context)!.confirm,
                                //       style: TextStyle(color: Colors.white),
                                //     ),
                                //   );
                                // },
                                listViewProps: ListViewProps(
                                  physics: BouncingScrollPhysics(),
                                ),
                              ),
                            ),
mazab99 commented 6 months ago

I fixed it by

compareFn: (item1, item2) => item1.name == item2.name,