如何在CupertinoPicker小部件中使用通知监听器?

时间:2019-09-09 11:17:08

标签: flutter

我正在尝试仅在CupertinoPicker的价格稳定下来时才能获取它的价值。文档指出:“要仅在滚动结束时获取值,请使用NotificationListener,监听ScrollEndNotification并读取其FixedExtentMetrics。”

这实际上如何工作?目前,我只是试图将我的CupertinoPicker小部件包装在NotificationListener中,并试图实现onNotification回调,但这是我遇到的问题。

1 个答案:

答案 0 :(得分:1)

听起来您快到了。这应该可以解决问题。

本文中的更多信息:https://medium.com/@diegoveloper/flutter-lets-know-the-scrollcontroller-and-scrollnotification-652b2685a4ac

NotificationListener<ScrollNotification>(
                onNotification: (scrollNotification){
                  if (scrollNotification is ScrollEndNotification) {

                    //Will only update when user has stopped scrolling in picker.
                    print(_pickerValue);

                    return true;
                  } else {
                    return false;
                  }
                },
                child: CupertinoPicker(
                  itemExtent: 30,
                  scrollController: scrollController,
                  children: <Widget>[
                    Text('First'),
                    Text('Second'),
                    Text('Third'),
                  ],
                  onSelectedItemChanged: (int value) {
                    setState(() {
                      //Will update every time picker value changes.
                      _pickerValue = scrollController.selectedItem;

                    });
                  },
                ),
              ),