我正在尝试使用PageView
来获取用户向哪个方向(向左或向右)滑动。
我能够得到这样的指示。
代码:
PageController _controller;
@override
void initState() {
_controller = new PageController()..addListener(_listener);
super.initState();
}
_listener() {
if (_controller.position.userScrollDirection == ScrollDirection.reverse) {
print('swiped to right');
} else {
print('swiped to left');
}
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(),
body: PageView.builder(
itemCount: 10,
controller: _controller,
itemBuilder: (context, index) {
return new Center(child: Text('item ${++index}'));
}),
);
}
但是,由于滚动还没有结束,所以
print
方法多次返回。
在当前页面完全切换到下一页后,有什么方法可以获取此信息?
flutter: swiped to right
flutter: swiped to right
flutter: swiped to right
flutter: swiped to right
flutter: swiped to right
flutter: swiped to right
flutter: swiped to right
flutter: swiped to right
flutter: swiped to right
flutter: swiped to right
flutter: swiped to right
最佳答案
将当前_controller.page.round()
与上一个监听器调用中的值进行比较(将先前的值存储在State
中)。
关于 flutter 如何获取最后的PageView滚动方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52203527/