Flutter listview builder滚动控制器侦听器不在列表视图中触发? [英] Flutter listview builder Scroll controller listener not firing inside list view?

查看:25
本文介绍了Flutter listview builder滚动控制器侦听器不在列表视图中触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个列表视图中有一个列表视图构建器小部件.当滚动位置到达其末尾时,内部列表视图侦听器不会触发.

I have a listview builder widget inside another list view. Inner listview listener is not firing when scrolling position reaches to its end.

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();
      },
   ),
  ]
 )
)

推荐答案

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

the list view must scroll otherwise it won't work. Not only you have to remove the NeverScrollableScrollPhysics() but also add that list view into some container and set its height smaller then overall height of your ListView. Then the listView begin to scroll and the function will be triggered

  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 builder滚动控制器侦听器不在列表视图中触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆