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

MaxSuggestionInViewport does not display correctly #152

Closed bebaoboy closed 4 months ago

bebaoboy commented 4 months ago

Describe the bug There are some bugs when setting the attribute maxSuggestionsInViewport.

To Reproduce

1. Incorrect Display

I have a simple search field like this with about 16 items. maxSuggestionsInViewPort is set to 5 by default. image However, the suggestion result in the app does not show 5 item. image


2. Does not have a lower bound like it says in the document.

image But I can still set it to a value smaller than 5. image image Again the maxSuggestionsInViewPort display incorrectly.

Suggested Solution: add a lowerbound check.


3. Upper bound?

So I try to set the value to 16, which is the number of my items (supposed to show JKL10 ... as well). This also happens with value that is bigger than the number of items. Result: the suggestion box does not show all my item. Is this an expected behaviour? image


[x] By clicking this checkbox, I confirm I am using the latest version of the package found on pub.dev/searchfield and from master branch on this repo. No library file has been edited.

Code sample

Show code sample ```dart SearchField( hint: 'Basic SearchField', maxSuggestionsInViewPort: 16, //<--- trouble initialValue: SearchFieldListItem('ABC'), suggestions: [ 'ABC', 'DEF', 'GHI', 'JKL', 'JKL1', 'JKL2', 'JKL3', 'JKL4', 'JKL5', 'JKL6', 'JKL7', 'JKL8', 'JKL9', 'JKL10', 'JKL11', 'JKL12', ].map(SearchFieldListItem.new).toList(), suggestionState: Suggestion.expand, ), ```
maheshj01 commented 4 months ago

Thanks for catching that, we definetly need to fix that logic and have basic sanity checks such as zero or negative height. Anyone affected by this issue can get around by adjusting the itemHeight property and maxSuggestionsInViewPort But I understand Searchfield should handle that by default.

bebaoboy commented 4 months ago

@maheshmnj can you please point me in a direction for the first bug (most important one) listed above? I'm not sure where or how that happened in the code. Thank you.

maheshj01 commented 4 months ago

@bebaoboy

Just adjust the itemHeight and maxSuggestionsInViewPort

Form(
  key: _formKey,
  child: SearchField(
    suggestions: _statesOfIndia
        .map((e) => SearchFieldListItem(e))
        .toList(),
    suggestionState: Suggestion.expand,
    textInputAction: TextInputAction.next,
    hint: 'SearchField Example 2',
    searchStyle: TextStyle(
      fontSize: 18,
      color: Colors.black.withOpacity(0.8),
    ),
    validator: (x) {
      if (!_statesOfIndia.contains(x) || x!.isEmpty) {
        return 'Please Enter a valid State';
      }
      return null;
    },
    searchInputDecoration: InputDecoration(
      focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(
          color: Colors.black.withOpacity(0.8),
        ),
      ),
      border: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.red),
      ),
    ),
    maxSuggestionsInViewPort: 6, // adjust the number of Suggestions
    itemHeight: 50,  // adjusts each suggestionsHeight
    onTap: (x) {},
  ))
bebaoboy commented 4 months ago

@maheshmnj sorry, i mean the code where the suggestion box height is calculating. Because in the first bug, I set maxHeight in view port = 5 but the box shows only 4 suggestions...

maheshj01 commented 4 months ago

This is where it is buggy https://github.com/maheshmnj/searchfield/blob/2925d80f9136c3bf1e873367c5a0ad33163575f8/lib/src/searchfield.dart#L628

maheshj01 commented 4 months ago

Fixed and Released in v1.0.7