Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On reaching the end of the scrollview using SingleChildScrollView - listener called twice

Tags:

scroll

flutter

I am using SingleChildScrollView and a scrollController to be notified when reaching the end of the scroll view

the SingleChildScrollView component:

 Expanded(
        child: SingleChildScrollView(
      controller: controller.scrollController,
      physics: const ScrollPhysics(),
      child: Center(
          child: Obx(() =>
              controller.busy && controller.itemList.isEmpty
                  ? loader
                  : Column(children: [
                      listWidget(controller.itemList),
                      if (controller.busy) loader
                    ]))),

the Listener:

      scrollController.addListener(() {
          if (scrollController.offset >=
                  scrollController.position.maxScrollExtent &&
              !scrollController.position.outOfRange) {
              _next();
            
}

but for some reason it is calling _next() twice when I am reaching the end of the scroll view and not once as it should. any idea why? am I doing something wrong?

like image 284
vigdora Avatar asked Nov 15 '25 11:11

vigdora


1 Answers

Use

physics: ClampingScrollPhysics()

in place of

physics: const ScrollPhysics(),

which will clamp the scroll when it reaches the end and will not bounce. The bounce will make the method call twice

like image 180
Kaushik Chandru Avatar answered Nov 17 '25 10:11

Kaushik Chandru