14

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();
      },
   ),
  ]
 )
)
3
  • is there any reason having ListView inside another ListView?.. It doesn't make any sense from your code Commented Jun 26, 2020 at 12:10
  • 1
    There is some more other widgets, I didn't include here Commented Jun 26, 2020 at 12:23
  • 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(). Commented Jan 20, 2021 at 13:34

2 Answers 2

16

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(),
          ],
        ),
      ),
Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

The question was about why not scrolling. Author did'nt asked to point how to reparent second ListView
@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 it
The question was not how make it scrollable, instead why it's not scrollable (no callbacks fired)
@SergeySalnikov please read the question carefully. I have highlighted the part for you in previous comment
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.