我正在尝试使用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}'));
}),
);
}
Run Code Online (Sandbox Code Playgroud)
但是,由于滚动还没有结束,print方法会多次返回。在当前页面完全切换到下一页后,我有什么办法可以得到这个吗?
flutter:向右
滑动
flutter:向右
滑动
flutter:向右
滑动
flutter:向右
滑动
flutter:向右
滑动 flutter:向右滑动 flutter:向右滑动 flutter:向右滑动
颤动:向右滑动
更好的解决方案如下,使用 PageView 小部件的内置功能。
PageView(
onPageChanged: (int page) {
// this page variable is the new page and will change before the pageController fully reaches the full, rounded int value
var swipingRight = page > pageController.page;
print(swipingRight);
},
Run Code Online (Sandbox Code Playgroud)
将当前值与先前侦听器调用的值进行比较_controller.page.round()(将先前值存储在 中State)。
| 归档时间: |
|
| 查看次数: |
8279 次 |
| 最近记录: |