Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么使swiper禁止上下滚动,只能左右滑动 #302

Open
beitangxx3yes opened this issue Mar 16, 2024 · 1 comment
Open

怎么使swiper禁止上下滚动,只能左右滑动 #302

beitangxx3yes opened this issue Mar 16, 2024 · 1 comment

Comments

@beitangxx3yes
Copy link

beitangxx3yes commented Mar 16, 2024

我尝试了 physics: const NeverScrollableScrollPhysics(), 之后发现左右滑动被禁止了,但仍然能上下滚动。在这之后尝试了在swiper外层套了一层 GestureDetector 并设置了 onVerticalDragUpdate: (detail){}, 还是没有效果。

这是我的代码:

  Widget _Category() {
    return SizedBox(
      width: ScreenAdapter.widht(1080),
      height: ScreenAdapter.height(400),
      child: Obx(()=>GestureDetector(
        onVerticalDragUpdate: (detail){},
        child: Swiper(
          physics: const NeverScrollableScrollPhysics(),
          scrollDirection:Axis.horizontal,
          autoplayDisableOnInteraction: true,
          itemCount: controller.categoryList.length~/10,
          pagination: SwiperPagination(
              margin: EdgeInsets.only(bottom: 10),
              builder: SwiperCustomPagination(
                  builder: (BuildContext context, SwiperPluginConfig config) {
                    return ConstrainedBox(
                      constraints:
                      BoxConstraints.expand(height: ScreenAdapter.height(30)),
                      child: Row(
                        children: <Widget>[
                          Expanded(
                            child: Align(
                              alignment: Alignment.center,
                              child: const RectSwiperPaginationBuilder(
                                  size: Size(20, 3),
                                  activeSize: Size(20, 3),
                                  color: Colors.black12,
                                  activeColor: Colors.black54)
                                  .build(context, config),
                            ),
                          )
                        ],
                      ),
                    );
                  })),
          loop: false,
          itemBuilder: (context, index) {
            return GridView.builder(
                itemCount: 10,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 5,
                    crossAxisSpacing: ScreenAdapter.widht(20),
                    mainAxisSpacing: ScreenAdapter.height(20)),
                itemBuilder: (context, i) {
                  String? pic = controller.categoryList[index*10+i].pic;
                  return Column(
                    children: [
                      Container(
                        alignment: Alignment.center,
                        width: ScreenAdapter.widht(116),
                        child: Image.network(
                          "https://miapp.itying.com/${pic!.replaceAll("\\", "/")}",
                          fit: BoxFit.fitWidth,
                        ),
                      ),
                      SizedBox(
                        width: ScreenAdapter.height(0),
                      ),
                      Text(
                        "${controller.categoryList[index*10+i].title}",
                        style: TextStyle(fontSize: ScreenAdapter.size(34)),
                      )
                    ],
                  );
                });
          },
        ),
      ))
    );
  }

@yuanzhiji
Copy link

在你的GridView下面添加physics: const NeverScrollableScrollPhysics(), 我是通过这个解决的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants