Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onSuggestionTap not giving back the value null value always #104

Closed
WaseemAbbasi22 opened this issue Nov 17, 2023 · 5 comments
Closed

onSuggestionTap not giving back the value null value always #104

WaseemAbbasi22 opened this issue Nov 17, 2023 · 5 comments
Labels
solved A solution has been proposed

Comments

@WaseemAbbasi22
Copy link

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
class LocationSearchAbleTextField extends StatelessWidget {
  final TextEditingController controller;
  final Function(dynamic val) onSuggestionItemTap;
  final List<dynamic> 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<ListingVm>(context, listen: false);
    final focus = FocusNode();
    return Center(
      child: SearchField(
        controller: controller,
        initialValue:
            initialValue != null && suggestionList.contains(initialValue)
                ? SearchFieldListItem<String>(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<String>(
                  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<String>(
                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<String> 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
Copy link
Author

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

@WaseemAbbasi22
Copy link
Author

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

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

i got null value

@maheshj01 maheshj01 added the in triage Issue is currently being triaged label Nov 17, 2023
@maheshj01
Copy link
Owner

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
Copy link
Owner

Let me know if you have any further questions.

@maheshj01 maheshj01 added the waiting for author waiting for author to respond back with more info label Nov 17, 2023
@WaseemAbbasi22
Copy link
Author

Thanks alot i got the point

@maheshj01 maheshj01 added solved A solution has been proposed and removed waiting for author waiting for author to respond back with more info in triage Issue is currently being triaged labels Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solved A solution has been proposed
Projects
None yet
Development

No branches or pull requests

2 participants