Flutter:如何禁用ListView的触摸滚动功能

154

有没有可能让 ListView 仅通过滚动控制器来滚动,而不使用触摸屏幕?


41
ListView中有一个字段physics = NeverScrollableScrollPhysics();现在你可以根据某些条件来实现它。 - Tree
你能详细说一下你尝试过什么,以及哪些方法没有奏效吗? - Edman
6个回答

258

如评论中所提到的,NeverScrollableScrollPhysics 类 可以实现此功能:

NeverScrollableScrollPhysics 类

不允许用户滚动的滚动物理效果。


1
非常感谢,这帮了我很多,也节省了我的时间。 - Ronak Patel

255
在 ListView 小部件内部,使用
physics: const NeverScrollableScrollPhysics()

52

您可以在ListView Widget中使用primary: false来设置。

默认匹配平台惯例。此外,如果primary为false,则当内容不足以滚动时,用户无法滚动,而如果primary为true,则他们始终可以尝试滚动。

更多信息,请查看官方文档


22

启用和禁用滚动视图的条件语句。

physics: chckSwitch ? const  NeverScrollableScrollPhysics() : const AlwaysScrollableScrollPhysics(),

17

对我来说起作用了

 ListView.builder(
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
    physics: const ClampingScrollPhysics(),
...

3
你好,欢迎来到 Stack Overflow!请参观一下 导览。感谢您提供答案,但您能否添加一些解释说明您的代码是如何解决问题的呢? - Jeanne Dark

5
嵌套滚动视图是什么?
            bottomNavigationBar: _buildBottomAppBar(),
            body: Container(
              child: NestedScrollView(
                physics: NeverScrollableScrollPhysics(),
                controller: _scrollViewController,
                headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                  return <Widget>[
                    buildSliverAppBar(innerBoxIsScrolled),
                  ];
                },
                body: _buildBody(context),
              ),
            ),
          );

对我来说它起作用了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接