Flutter教程:多个infinity_slider嵌套实现滑动互联的效果

2019-09-0318:40:39APP与小程序开发Comments3,176 views字数 1998阅读模式

目标

  • 多个infinity_slider嵌套实现滑动互联的效果

完整代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16037.html

思路

  1. 通过widget树找到父infinity_slider控件
  2. 使用NotificationListener监听当前infinity_slider控件滚动
  3. 使用父infinity_slider控件的_pageController移动父infinity_slider控件的位置
  4. 禁用当前infinity_slider控件在父infinity_slider控件未抵达边缘时产生的光晕效果

步骤

1) 找到父infinity_slider控件

InfinitySlider类中添加此代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16037.html

static InfinitySliderState of(BuildContext context) {
    return context.ancestorStateOfType(TypeMatcher<InfinitySliderState>());
}
复制代码

initState中初始化下方代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16037.html

InfinitySliderState _ancestor;

@override
void initState() {
    _linkAncestorIfNeeded();
}
void _linkAncestorIfNeeded() {
    _ancestor = InfinitySlider.of(context);
}
复制代码

2) 监听当前infinity_slider控件滚动

@override
Widget build(BuildContext context) {
    return NotificationListener(
        onNotification: _handleScrollNotification,
        child: PageView.builder(
            ......
        )
    );
}

void _handleScrollNotification(ScrollNotification notification) {
    
}
复制代码

3) 控制父infinity_slider控件滚动

bool _handleScrollNotification(ScrollNotification notification) {
    if (notification is OverscrollNotification && _ancestor != null) {
      if (_canLinkWithAncestorScroll(notification.overscroll < 0)) {
        _ancestor._pageController.position
            .moveTo(_ancestor._pageController.offset + notification.overscroll);
      }
    }
}
  
bool _canLinkWithAncestorScroll(bool onLeftEdge) {
    if (_ancestor == null) return false;
    return (onLeftEdge &&
        _ancestor._pageController.offset !=
            _ancestor._pageController.position.minScrollExtent) ||
        ((!onLeftEdge &&
            _ancestor._pageController.offset !=
                _ancestor._pageController.position.maxScrollExtent));
}
复制代码

4) 禁用infinity_slider控件在与父infinity_slider控件联动产生的光晕

@override
Widget build(BuildContext context) {
    return NotificationListener(
      onNotification: _handleScrollNotification,
      child: NotificationListener(
            onNotification: _handleGlowNotification,
            child: PageView.builder(
                ......
            )
        )
    );
)

bool _handleGlowNotification(OverscrollIndicatorNotification notification) {
    if (notification.depth == 0 &&
        _canLinkWithAncestorScroll(notification.leading)) {
      notification.disallowGlow();
      return true;
    }
    return false;
}

作者:么姐
链接:https://juejin.im/post/5d6c973ae51d45620064bb92
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16037.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16037.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/xcx/16037.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定