Let user settle picking value from CupertinoPicker(onSelectedItemChanged ), after that it should send call to API

255

You can check if the metrics of the scrollNotification are of type FixedExtentMetrics. This type has the value itemIndex which you can use to determine which item is currently selected.

  return NotificationListener<ScrollNotification>(
    onNotification: (scrollNotification) {
      if (scrollNotification is ScrollEndNotification &&
      scrollNotification.metrics is FixedExtentMetrics) {
        (scrollNotification.metrics as FixedExtentMetrics).itemIndex; // Index of the list
        return true;
      } else {
        return false;
      }
    },
Share:
255
Usama Karim
Author by

Usama Karim

Updated on December 24, 2022

Comments

  • Usama Karim
    Usama Karim over 1 year

    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,
          ),
        );
      }