Flutter 颤振始终滚动滚动物理()不工作 问题:
您好,我正在搜索一个解决方案,允许用户在列表上滚动,即使没有足够的内容。 通过查看Flitter文档,我找到了这一页 正如文件所说 要强制滚动视图始终可滚动(即使内容不足),就像primary为true但不必将其设置为true一样,请提供AlwaysScrollableScrollPhysics物理对象,如中所示: 所以我试着运行一个简单的代码来检测用户滚动,即使没有足够的内容 代码Flutter 颤振始终滚动滚动物理()不工作 问题:,flutter,Flutter,您好,我正在搜索一个解决方案,允许用户在列表上滚动,即使没有足够的内容。 通过查看Flitter文档,我找到了这一页 正如文件所说 要强制滚动视图始终可滚动(即使内容不足),就像primary为true但不必将其设置为true一样,请提供AlwaysScrollableScrollPhysics物理对象,如中所示: 所以我试着运行一个简单的代码来检测用户滚动,即使没有足够的内容 代码 类页面扩展StatefulWidget{ @凌驾 _PageState createState(); } 类Pa
类页面扩展StatefulWidget{
@凌驾
_PageState createState();
}
类PageState扩展了状态{
@凌驾
小部件构建(构建上下文){
最终ScrollController ScrollController=ScrollController();
@凌驾
void initState(){
scrollController.addListener((){
打印('调用侦听器');
});
super.initState();
}
返回脚手架(
正文:ListView.builder(
控制器:滚动控制器,
物理:常量AlwaysScrollablePhysics(),
物品计数:5,
itemBuilder:(上下文,索引){
返回填充(
填充:仅限常量边集(底部:8.0),
子:容器(
颜色:颜色,黑色,
身高:50,
),
);
},
),
);
}
}
为什么这不起作用
编辑
这是我期待的设计
我有一个动态创建的列表。我希望能够检测用户在该列表上的垂直滑动,即使没有滚动,因为没有足够的元素溢出屏幕高度
在可滚动列表上,我可以简单地添加一个滚动侦听器,然后每次检测到滚动时,我都可以使用scrollController.position info执行逻辑。
我希望即使当用户在这种类型的列表上滑动时也能调用scroll listener,我确实看到了scroll的效果,添加了AlwaysScrollableScrollPhysics,因此该部分似乎工作正常。也许在NotificationListener上包装脚手架可以完成您想要做的事情:
class _PageState extends State<Page> {
@override
Widget build(BuildContext context) {
final ScrollController scrollController = ScrollController();
return NotificationListener(
child: Scaffold(
body: ListView.builder(
controller: scrollController,
physics: const AlwaysScrollableScrollPhysics(),
itemCount: 5,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(
color: Colors.black,
height: 50,
),
);
},
),
),
onNotification: (scrollNotification) {
if (scrollNotification is ScrollStartNotification) {
print('Widget has started scrolling');
}
return true;
},
);
}
}
class\u页面状态扩展状态{
@凌驾
小部件构建(构建上下文){
最终ScrollController ScrollController=ScrollController();
返回通知侦听器(
孩子:脚手架(
正文:ListView.builder(
控制器:滚动控制器,
物理:常量AlwaysScrollablePhysics(),
物品计数:5,
itemBuilder:(上下文,索引){
返回填充(
填充:仅限常量边集(底部:8.0),
子:容器(
颜色:颜色,黑色,
身高:50,
),
);
},
),
),
onNotification:(滚动通知){
如果(scrollNotification为ScrollStartNotification){
打印(“小部件已开始滚动”);
}
返回true;
},
);
}
}
NotificationListener有一个名为onNotification的属性,允许您检查不同类型的滚动通知,您可以在此处查看更多信息:您能否解释一下“即使内容不足也可以滚动列表”?你想要的确切结果是什么?有什么设计/gif可以显示吗?我编辑了这个线程。希望我很清楚谢谢,这就是解决办法。添加通知侦听器允许我捕获任何事件。
class Page extends StatefulWidget {
@override
_PageState createState() => _PageState();
}
class _PageState extends State<Page> {
@override
Widget build(BuildContext context) {
final ScrollController scrollController = ScrollController();
@override
void initState(){
scrollController.addListener((){
print('listener called');
});
super.initState();
}
return Scaffold(
body: ListView.builder(
controller: scrollController,
physics: const AlwaysScrollableScrollPhysics(),
itemCount: 5,
itemBuilder: (context, index){
return Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(
color: Colors.black,
height: 50,
),
);
},
),
);
}
}
class _PageState extends State<Page> {
@override
Widget build(BuildContext context) {
final ScrollController scrollController = ScrollController();
return NotificationListener(
child: Scaffold(
body: ListView.builder(
controller: scrollController,
physics: const AlwaysScrollableScrollPhysics(),
itemCount: 5,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(
color: Colors.black,
height: 50,
),
);
},
),
),
onNotification: (scrollNotification) {
if (scrollNotification is ScrollStartNotification) {
print('Widget has started scrolling');
}
return true;
},
);
}
}