Flutter:ListView 禁用触摸屏滚动
Flutter: ListView disable scrolling with touchscreen
是否可以让 ListView 只能通过 ScrollController 滚动,而不能通过触摸屏滚动?
如评论中所述,NeverScrollableScrollPhysics class 将执行此操作:
NeverScrollableScrollPhysics class
Scroll physics that does not allow the user to scroll.
在 ListView 小部件中,使用
physics: const NeverScrollableScrollPhysics()
您可以在 ListView 小部件中添加 primary: false
默认匹配平台约定。此外,如果 primary 为 false,则如果没有足够的内容可滚动,则用户无法滚动,而如果 primary 为 true,则他们始终可以尝试滚动。
有关更多信息,请查看 Official Doc
启用和禁用滚动视图的条件语句。
physics: chckSwitch ? const NeverScrollableScrollPhysics() : const AlwaysScrollableScrollPhysics(),
为我工作
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: const ClampingScrollPhysics(),
...
NestedScrollView 呢?
bottomNavigationBar: _buildBottomAppBar(),
body: Container(
child: NestedScrollView(
physics: NeverScrollableScrollPhysics(),
controller: _scrollViewController,
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
buildSliverAppBar(innerBoxIsScrolled),
];
},
body: _buildBody(context),
),
),
);
对我有用
您可以通过两种方式实现此目的
如果您只想禁用整个屏幕的滚动
然后解决方案是将 ListView
包装在 IgnorePointer
小部件中。
你也可以像这样
只在你的ListView
上禁用滚动
在 ListView 上设置 shrinkWrap: true
和 primary: true
属性,这将禁用滚动:
ListView(
shrinkWrap: true,
primary: true,
是否可以让 ListView 只能通过 ScrollController 滚动,而不能通过触摸屏滚动?
如评论中所述,NeverScrollableScrollPhysics class 将执行此操作:
NeverScrollableScrollPhysics class
Scroll physics that does not allow the user to scroll.
在 ListView 小部件中,使用
physics: const NeverScrollableScrollPhysics()
您可以在 ListView 小部件中添加 primary: false
默认匹配平台约定。此外,如果 primary 为 false,则如果没有足够的内容可滚动,则用户无法滚动,而如果 primary 为 true,则他们始终可以尝试滚动。
有关更多信息,请查看 Official Doc
启用和禁用滚动视图的条件语句。
physics: chckSwitch ? const NeverScrollableScrollPhysics() : const AlwaysScrollableScrollPhysics(),
为我工作
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: const ClampingScrollPhysics(),
...
NestedScrollView 呢?
bottomNavigationBar: _buildBottomAppBar(),
body: Container(
child: NestedScrollView(
physics: NeverScrollableScrollPhysics(),
controller: _scrollViewController,
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
buildSliverAppBar(innerBoxIsScrolled),
];
},
body: _buildBody(context),
),
),
);
对我有用
您可以通过两种方式实现此目的
如果您只想禁用整个屏幕的滚动
然后解决方案是将
ListView
包装在IgnorePointer
小部件中。你也可以像这样
只在你的ListView
上禁用滚动在 ListView 上设置
shrinkWrap: true
和primary: true
属性,这将禁用滚动:ListView( shrinkWrap: true, primary: true,