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

170

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

Share:
170
vigdora
Author by

vigdora

Updated on January 01, 2023

Comments

  • vigdora
    vigdora over 1 year

    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?