I am using CupertinoWidget for iOS users to scroll through List and check the price of a currency. But when scroll happens, onSelectedItemChanged sends callback to API for every value from the list. I read the document but unable to understand what to do. It pleasing if there is an example.
In document it's mentioned as CupertinoPicker > onSelectedItemChanged property
This can be called during scrolls and during ballistic flings. To get the value only when the scrolling settles, use a NotificationListener, listen for ScrollEndNotification and read its FixedExtentMetrics.
NotificationListener cupertinoPickerList() {
List<Text> textWidgetList = [];
for (String curreny in currenciesList) {
textWidgetList.add(
Text(
curreny,
style: TextStyle(
color: Colors.white,
),
),
);
}
return NotificationListener<ScrollNotification>(
onNotification: (scrollNotification) {
if (scrollNotification is ScrollEndNotification) {
return true;
} else {
return false;
}
},
child: CupertinoPicker(
itemExtent: 30,
scrollController: FixedExtentScrollController(initialItem: 19),
onSelectedItemChanged: (selectedIndex) {
selectedCurreny = currenciesList[selectedIndex];
updateUI(selectedCurreny);
print(selectedCurreny);
},
children: textWidgetList,
),
);
}
You can check if the
metrics
of thescrollNotification
are of typeFixedExtentMetrics
. This type has the value itemIndex which you can use to determine which item is currently selected.