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

2

13

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();
      },
   ),
  ]
 )
)
Viperish answered 26/6, 2020 at 12:9 Comment(3)
is there any reason having ListView inside another ListView?.. It doesn't make any sense from your codeVentricular
There is some more other widgets, I didn't include hereViperish
You are extending your ListView.builder() inside the ListView() that have infinite height so scroll view can not find the max scrolling extent.So try to remove parent ListView().Britain
H
17

You 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(),
          ],
        ),
      ),
Hypermetropia answered 16/4, 2021 at 9:33 Comment(0)
V
6

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()));
              },
            ),
          ),
        ],
      ),
    );
  }
Ventricular answered 26/6, 2020 at 12:47 Comment(5)
The question was about why not scrolling. Author did'nt asked to point how to reparent second ListViewStressful
@SergeySalnikov the OP problem was Inner listview listener is not firing when scrolling position reaches to its end which means the inner ListView was expanded to parent Listview which means the inner ListView never scrolled but the parent ListView which means it could not fire itVentricular
The question was not how make it scrollable, instead why it's not scrollable (no callbacks fired)Stressful
@SergeySalnikov please read the question carefully. I have highlighted the part for you in previous commentVentricular
Thanks! This was exactly the problem in my case. But I solved it by dynamically calculating the fetch limit for pagination using screen height, and fetched little more than required so that I always have items to scroll.Schaab

© 2022 - 2024 — McMap. All rights reserved.