@@ -19,11 +19,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
19
19
required this .menuItemStyle,
20
20
required this .searchData,
21
21
this .dropdownSeparator,
22
- }) : itemHeights = addSeparatorsHeights (
23
- itemHeights: items.map ((item) => item.height).toList (),
24
- separatorHeight: dropdownSeparator? .height,
25
- ),
26
- barrierColor = barrierCoversButton ? barrierColor : null ,
22
+ }) : barrierColor = barrierCoversButton ? barrierColor : null ,
27
23
_altBarrierColor = barrierColor;
28
24
29
25
final List <DropdownItem <T >> items;
@@ -40,7 +36,6 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
40
36
final DropdownSearchData <T >? searchData;
41
37
final DropdownSeparator <T >? dropdownSeparator;
42
38
43
- final List <double > itemHeights;
44
39
ScrollController ? scrollController;
45
40
46
41
@override
@@ -114,17 +109,32 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
114
109
}
115
110
}
116
111
112
+ double _itemHeightWithSeparator (int itemIndex) {
113
+ final itemHeight = items[itemIndex].height;
114
+ final separatorHeight = dropdownSeparator? .height ?? 0 ;
115
+ return itemIndex != 0 ? itemHeight + separatorHeight : itemHeight;
116
+ }
117
+
118
+ double _calculateHeightUntilIndex (
119
+ int index, {
120
+ bool Function (DropdownItem <T > item)? itemPredicate,
121
+ }) {
122
+ var itemsHeight = 0.0 ;
123
+ for (int i = 0 ; i < index; i++ ) {
124
+ if (itemPredicate == null || itemPredicate (items[i])) {
125
+ itemsHeight += _itemHeightWithSeparator (i);
126
+ }
127
+ }
128
+ return itemsHeight;
129
+ }
130
+
117
131
double getItemOffset (int index) {
118
132
final double paddingTop = dropdownStyle.padding != null
119
133
? dropdownStyle.padding! .resolve (null ).top
120
134
: kMaterialListPadding.top;
121
135
double offset = paddingTop;
122
136
123
137
if (items.isNotEmpty && index > 0 ) {
124
- assert (
125
- items.length + (dropdownSeparator != null ? items.length - 1 : 0 ) ==
126
- itemHeights.length,
127
- );
128
138
if (searchData? .searchController? .text case final searchText? ) {
129
139
final searchMatchFn =
130
140
searchData? .searchMatchFn ?? _defaultSearchMatchFn ();
@@ -133,24 +143,19 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
133
143
offset += _getSearchItemsHeight (index, searchText);
134
144
}
135
145
} else {
136
- for (int i = 0 ; i < index; i++ ) {
137
- offset += itemHeights[i];
138
- }
146
+ offset += _calculateHeightUntilIndex (index);
139
147
}
140
148
}
141
149
142
150
return offset;
143
151
}
144
152
145
153
double _getSearchItemsHeight (int index, String searchText) {
146
- var itemsHeight = 0.0 ;
147
154
final searchMatchFn = searchData? .searchMatchFn ?? _defaultSearchMatchFn ();
148
- for (int i = 0 ; i < index; i++ ) {
149
- if (searchMatchFn (items[i], searchText)) {
150
- itemsHeight += itemHeights[i];
151
- }
152
- }
153
- return itemsHeight;
155
+ return _calculateHeightUntilIndex (
156
+ index,
157
+ itemPredicate: (item) => searchMatchFn (item, searchText),
158
+ );
154
159
}
155
160
156
161
// Returns the vertical extent of the menu and the initial scrollOffset
@@ -177,7 +182,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
177
182
final searchText = searchData? .searchController? .text;
178
183
actualMenuHeight += searchText != null
179
184
? _getSearchItemsHeight (items.length, searchText)
180
- : itemHeights. reduce (( double total, double height) => total + height );
185
+ : _calculateHeightUntilIndex (items.length );
181
186
}
182
187
183
188
// Use actualMenuHeight if it's less than maxHeight.
@@ -215,13 +220,11 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
215
220
final double actualMenuNetHeight = actualMenuHeight - innerWidgetHeight;
216
221
// The offset should be zero if the selected item is in view at the beginning
217
222
// of the menu. Otherwise, the scroll offset should center the item if possible.
218
- final actualIndex = dropdownSeparator? .height != null ? index * 2 : index;
219
- final double selectedItemOffset = getItemOffset (actualIndex);
223
+ final double selectedItemOffset = getItemOffset (index);
220
224
scrollOffset = math.max (
221
- 0.0 ,
222
- selectedItemOffset -
223
- (menuNetHeight / 2 ) +
224
- (itemHeights[actualIndex] / 2 ));
225
+ 0.0 ,
226
+ selectedItemOffset - (menuNetHeight / 2 ) + (items[index].height / 2 ),
227
+ );
225
228
// If the selected item's scroll offset is greater than the maximum scroll offset,
226
229
// set it instead to the maximum allowed scroll offset.
227
230
final double maxScrollOffset = actualMenuNetHeight - menuNetHeight;
0 commit comments