gpt4 book ai didi

Flutter ListView 构建器滚动 Controller 监听器未在 ListView 内触发?

转载 作者:行者123 更新时间:2023-12-03 03:33:13 25 4
gpt4 key购买 nike

我在另一个 ListView 中有一个 ListView 构建器小部件。当滚动位置到达其末尾时,内部 ListView 监听器不会触发。

initState() {
super.initState();

_scrollController.addListener(() {
if (_scrollController.position.maxScrollExtent ==
_scrollController.position.pixels) {function();}
}

Container(
child: Listview(
children: <Widget>[
Container(),
ListView.builder(
controller: _scrollController,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: list.length,
itemBuilder: (BuildContext context, int index) {
return Container();
},
),
]
)
)

最佳答案

ListView 必须滚动,否则将无法工作。您不仅必须删除 NeverScrollableScrollPhysics(),还要将该 ListView 添加到某个容器中,并将其高度设置为小于 ListView 的整体高度。然后listView开始滚动,函数就会被触发

  ScrollController _scrollController = ScrollController();
List<int> list = [1, 2, 3, 4, 5];
initState() {
super.initState();

_scrollController.addListener(() {
if (_scrollController.position.maxScrollExtent ==
_scrollController.position.pixels) {
print('firing');
}
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: ControlBar(
title: Text('Home'),
),
),
body: ListView(
children: <Widget>[
Container(
height: 150,
child: ListView.builder(
controller: _scrollController,
shrinkWrap: true,
itemCount: list.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(title: Text(list[index].toString()));
},
),
),
],
),
);
}

关于Flutter ListView 构建器滚动 Controller 监听器未在 ListView 内触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62594443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com