Flutter listview builder Scroll controller listener not firing inside list view?

5,402

Solution 1

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

Solution 2

U might have SingleChildScrollView attached before any widget :

so attach _scrollController to singleChildScrollView not listview

body: SingleChildScrollView(
        controller: _scrollController,
        child: Column(
          children: [
            _chips(),
            SizedBox(
              height: 10,
            ),
            _slider(),
            _showGrid(),
          ],
        ),
      ),

Thanks in advance if it's helpful to you !

Share:
5,402
afsal.an
Author by

afsal.an

Updated on December 21, 2022

Comments

  • afsal.an
    afsal.an over 1 year

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