selectedItem property
The currently selected item index that's closest to the center of the viewport.
There are circumstances that this FixedExtentScrollController can't know the current item. Reading selectedItem will throw an AssertionError in the following cases:
- No scroll view is currently using this FixedExtentScrollController.
- More than one scroll views using the same FixedExtentScrollController.
The hasClients property can be used to check if a scroll view is attached prior to accessing selectedItem.
Implementation
int get selectedItem {
assert(
positions.isNotEmpty,
'FixedExtentScrollController.selectedItem cannot be accessed before a '
'scroll view is built with it.',
);
assert(
positions.length == 1,
'The selectedItem property cannot be read when multiple scroll views are '
'attached to the same FixedExtentScrollController.',
);
final _FixedExtentScrollPosition position = this.position as _FixedExtentScrollPosition;
return position.itemIndex;
}