如何解决带有 CarouselSlider 的颤动 InteractiveViewer 很难捏缩放
我有一个 Flutter 应用程序,它显示带有 CarouselSlider 和 InteractiveViewer 的图像,因此用户可以捏合来放大和缩小。问题是这样做非常困难,通常捏合运动会使 CarouselSlider 在照片之间滑动。一种解决方案是使用 neverscrollablescrollphysics() 禁用幻灯片,但我不想这样做。
这是代码的一部分:
CarouselSlider(
items: widget.photos.map((i) {
return Container(
margin: EdgeInsets.all(10),child: InteractiveViewer(
scaleEnabled: true,minScale: 0.1,maxScale: 10.0,child: Image.network(i),),);
}).toList(),options: CarouselOptions(
enableInfiniteScroll: false,height: MediaQuery.of(context).size.height,disableCenter: true,viewportFraction: 1.0,initialPage: widget.position,onPageChanged: (index,reason) {
setState(() {
widget.position = index;
});
},
解决方法
这是第一个快速修复(我会尽快更新)
您可以复制 CarouselSlider-Class 并将其命名为 MyCarouselSlider
然后像这样编辑 getGestrureWrapper-Method
Widget getGestureWrapper(Widget child) {
Widget wrapper;
if (widget.options.height != null) {
wrapper = Container(height: widget.options.height,child: child);
} else {
wrapper = AspectRatio(aspectRatio: widget.options.aspectRatio,child: child);
}
return GestureDetector(
child: NotificationListener(
onNotification: (dynamic notification) {
if (widget.options.onScrolled != null && notification is ScrollUpdateNotification) {
widget.options.onScrolled(carouselState.pageController.page);
}
return false;
},child: wrapper,),behavior: HitTestBehavior.opaque,dragStartBehavior: DragStartBehavior.start,);
// return RawGestureDetector(
// behavior: HitTestBehavior.translucent,// gestures: {
// _MultipleGestureRecognizer: GestureRecognizerFactoryWithHandlers<_MultipleGestureRecognizer>(
// () => _MultipleGestureRecognizer(),(_MultipleGestureRecognizer instance) {
// instance.onStart = (_) {
// onPanDown();
// };
// instance.onDown = (_) {
// onPanDown();
// };
// instance.onEnd = (_) {
// onPanUp();
// };
// instance.onCancel = () {
// onPanUp();
// };
// }),// },// child: NotificationListener(
// onNotification: (dynamic notification) {
// if (widget.options.onScrolled != null && notification is ScrollUpdateNotification) {
// widget.options.onScrolled(carouselState.pageController.page);
// }
// return false;
// },// child: wrapper,// ),// );
}
并改用您新创建的 MyCarouselSlider-Class。 重要提示:这不是最终解决方案。
我不确定 RawGestureDetector 有什么问题。如果我找到更好的解决方案,我会更新这篇文章。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。