maheshj01 / searchfield

A highly customizable simple and easy to use flutter Widget to add a searchfield to your Flutter Application.This Widget allows you to search and select from list of suggestions.
MIT License
84 stars 63 forks source link

onSuggestionTap not giving back the value null value always #104

Closed WaseemAbbasi22 closed 1 year ago

WaseemAbbasi22 commented 1 year ago

I am using this package to show the dynamic list of object but it's just setting the value in the controller i didnot get back the suggestion tapped value onSuggestionCall give null value always

To Reproduce

Expected behavior A clear and concise description of what you expected to happen.

Actual behavior What you actually saw instead of the expected behavior.

Screenshots If applicable, add screenshots to help explain your problem.

Code sample

Show code sample ```dart class LocationSearchAbleTextField extends StatelessWidget { final TextEditingController controller; final Function(dynamic val) onSuggestionItemTap; final List suggestionList; final String hintText; VoidCallback? onTralingTap; String? initialValue; String? searchLabel; String? errorText; bool? showTraling; bool? isForm = false; double? itemHeight; String? Function(String?)? validator; Function(String?)? onSave; LocationSearchAbleTextField( {super.key, required this.controller, required this.onSuggestionItemTap, required this.suggestionList, this.onSave, this.isForm = false, this.initialValue, this.showTraling = true, this.validator, this.itemHeight, this.errorText, this.searchLabel, this.onTralingTap, required this.hintText}); @override Widget build(BuildContext context) { ListingVm listingVm = Provider.of(context, listen: false); final focus = FocusNode(); return Center( child: SearchField( controller: controller, initialValue: initialValue != null && suggestionList.contains(initialValue) ? SearchFieldListItem(initialValue!) : null, onSubmit: onSave, validator: validator, onSearchTextChanged: (query) { isForm! ? listingVm.getFormLocationList(query: query) : listingVm.getFilterLocationList(query: query); final filter = suggestionList .where((element) => searchLabel != null ? element.fullName.toLowerCase().contains(query.toLowerCase()) : element.name!.toLowerCase().contains(query.toLowerCase())) .toList(); return filter .map((e) => SearchFieldListItem( searchLabel != null ? e.fullName : e.name!, child: Padding( padding: const EdgeInsets.symmetric( vertical: 4.0, horizontal: 5.0), child: Text(searchLabel != null ? e.fullName : e.name!, style: AppStyle.lightDarkTextStyle.copyWith( fontSize: 1.8.h, fontWeight: FontWeight.normal)), ))) .toList(); }, key: const Key('searchfield'), hint: hintText, itemHeight: itemHeight ?? 6.h, autoCorrect: false, searchInputDecoration: InputDecoration( filled: true, errorText: errorText, contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 10), fillColor: AppColors.kScaffoldBackgroundColor, border: OutlineInputBorder( borderRadius: BorderRadius.circular(15.0), borderSide: BorderSide.none, ), suffix: showTraling! ? InkWell( onTap: onTralingTap ?? () async { controller.clear(); }, child: Icon( Icons.clear, color: Colors.black, size: 2.4.h, )) : null, hintStyle: TextStyle( color: AppColors.kBlackLightColor, fontSize: 2.h, ), hintText: hintText, ), suggestionsDecoration: suggestionDecoration, scrollbarAlwaysVisible: false, maxSuggestionsInViewPort: 5, ///line color between suggestion.. // marginColor: , suggestions: suggestionList .map((e) => SearchFieldListItem( searchLabel != null ? e.fullName : e.name!, child: Padding( padding: const EdgeInsets.symmetric( vertical: 4.0, horizontal: 4.0), child: Text(searchLabel != null ? e.fullName : e.name!, style: AppStyle.lightDarkTextStyle.copyWith( fontSize: 1.8.h, fontWeight: FontWeight.normal)), ))) .toList(), focusNode: focus, suggestionState: Suggestion.expand, onSuggestionTap: (SearchFieldListItem x) { focus.unfocus(); onSuggestionItemTap(controller.text); }, ), ); } final suggestionDecoration = SuggestionDecoration( shape: BoxShape.rectangle, boxShadow: const [ BoxShadow( color: AppColors.kScaffoldBackgroundColor, blurRadius: 2.0, spreadRadius: 0.0, offset: Offset(0.0, 0.0), ) ], padding: const EdgeInsets.all(4), color: AppColors.kWhiteColor, // borderRadius: BorderRadius.all(Radius.circular(20) // ) ); // final inputDec } ```

Additional context Add any other context about the problem here.

WaseemAbbasi22 commented 1 year ago

i need full object value i tried to change the type from string to dynamic but nothing changed alwasy got null values

WaseemAbbasi22 commented 1 year ago

onSuggestionTap: (SearchFieldListItem selectedItem) { print('i am here on suggestion tap...');

   print('selected item i got is ${selectedItem.item}');

    },

i got null value

maheshj01 commented 1 year ago

Hi @WaseemAbbasi22, Thanks for filing the issue. I see the issue in your code, you are not passing item parameters to SearchFieldListItem

return filter
              .map((e) => SearchFieldListItem<String>(
                  item: e,  // Pass the object you want to receive for the list item
                  searchLabel != null ? e.fullName : e.name!,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(
                        vertical: 4.0, horizontal: 5.0),
                    child: Text(searchLabel != null ? e.fullName : e.name!,
                        style: AppStyle.lightDarkTextStyle.copyWith(
                            fontSize: 1.8.h, fontWeight: FontWeight.normal)),
                  )))
              .toList();

See this complete example for more details https://github.com/maheshmnj/searchfield/blob/master/example/lib/country_search.dart

Additionally I have updated the doc comments a little bit to clarify this.

maheshj01 commented 1 year ago

Let me know if you have any further questions.

WaseemAbbasi22 commented 1 year ago

Thanks alot i got the point