我正在尝试仅在CupertinoPicker的价格稳定下来时才能获取它的价值。文档指出:“要仅在滚动结束时获取值,请使用NotificationListener,监听ScrollEndNotification
并读取其FixedExtentMetrics。”
这实际上如何工作?目前,我只是试图将我的CupertinoPicker小部件包装在NotificationListener
中,并试图实现onNotification
回调,但这是我遇到的问题。
答案 0 :(得分:1)
听起来您快到了。这应该可以解决问题。
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;
});
},
),
),