Skip to content

Commit

Permalink
Refactor getSearchItemsHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedLSayed9 committed Jun 1, 2024
1 parent a4e70d0 commit a6695b6
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions packages/dropdown_button2/lib/src/dropdown_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,33 +125,27 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
items.length + (dropdownSeparator != null ? items.length - 1 : 0) ==
itemHeights.length,
);
final double? searchOffset = _getSearchItemsHeight(endIndex: index);
if (searchOffset != null) {
offset += searchOffset;
if (searchData?.searchController?.text case final searchText?) {
offset += _getSearchItemsHeight(index, searchText);
} else {
offset += itemHeights
.sublist(0, index)
.reduce((double total, double height) => total + height);
for (int i = 0; i < index; i++) {
offset += itemHeights[i];
}
}
}

return offset;
}

double? _getSearchItemsHeight({int? endIndex}) {
final String? currentSearch = searchData?.searchController?.text;
if (currentSearch == null || currentSearch.isEmpty) {
return null;
}
double height = 0.0;
final int limit = endIndex ?? items.length;
double _getSearchItemsHeight(int index, String searchText) {
var itemsHeight = 0.0;
final searchMatchFn = searchData?.searchMatchFn ?? _defaultSearchMatchFn();
for (int index = 0; index < limit; ++index) {
if (searchMatchFn(items[index], currentSearch)) {
height += itemHeights[index];
for (int i = 0; i < index; i++) {
if (searchMatchFn(items[i], searchText)) {
itemsHeight += itemHeights[i];
}
}
return height;
return itemsHeight;
}

// Returns the vertical extent of the menu and the initial scrollOffset
Expand All @@ -175,13 +169,10 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
final double innerWidgetHeight = searchData?.searchBarWidgetHeight ?? 0.0;
actualMenuHeight += innerWidgetHeight;
if (items.isNotEmpty) {
final double? searchHeight = _getSearchItemsHeight();
if (searchHeight != null) {
actualMenuHeight += searchHeight;
} else {
actualMenuHeight +=
itemHeights.reduce((double total, double height) => total + height);
}
final searchText = searchData?.searchController?.text;
actualMenuHeight += searchText != null
? _getSearchItemsHeight(items.length, searchText)
: itemHeights.reduce((double total, double height) => total + height);
}

// Use actualMenuHeight if it's less than maxHeight.
Expand Down

0 comments on commit a6695b6

Please sign in to comment.