gpt4 book ai didi

flutter 如何获取最后的PageView滚动方向

转载 作者:行者123 更新时间:2023-12-03 13:30:46 26 4
gpt4 key购买 nike

我正在尝试使用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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com