问题描述
我在PageView.builder
中有一个StatelessWidget
。每个页面都有一个SwipeGestureRecognizer
,它在onSwipeDown上提供一个Navigator.pop(context)
。但我希望此手势仅在用户回滚到initialPage: 0
时触发。对于其余页面,我想使用PageView.builder
提供的默认滚动。我已经设法使它几乎起作用了,除了SwipeGestureRecognizer
的出现似乎使所有其他页面上的PageViewer的正常向下滑动默认滚动无效。如何获得向下滑动手势以仅在首页上激活?
class StageBuilder extends StatelessWidget {
final List<SpeakContent> speakcrafts;
StageBuilder(this.speakcrafts);
final PageController controller = PageController(initialPage: 0);
@override
Widget build(context) {
return PageView.builder(
controller: controller,itemCount: speakcrafts.length,itemBuilder: (context,int currentIndex) {
return createViewItem(speakcrafts[currentIndex],context,currentIndex);
},);
}
Widget createViewItem(SpeakContent speakcraft,BuildContext context,int currentIndex) {
return (
Container(
child: SwipeGestureRecognizer(some stuff)
),);
}
}
onSwipeDown:() {
if (currentIndex==0)
Navigator.pop(context);
},
解决方法
这就是我所做的;
child: SwipeGestureRecognizer(
onSwipeUp: () {
controller.nextPage(duration: const Duration(milliseconds: 400),curve: Curves.ease);
},onSwipeDown: () {
(currentIndex == 0)?
Navigator.pop(context):
controller.previousPage(duration: const Duration(milliseconds: 400),curve: Curves.ease);
},
...)