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

Viewed 358

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?

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

Related