skip to Main Content

I am developing one chat app and I am adding data to chatList which is the list of model and I am getting the newest data from the backend like when I call API it will give the last message so I need to get data from pagination without using any lazy loading method or something so for that I need to call API when I get data to get more data and my list is reverse list (I have UI different so I can’t use reverse property in listview) so I need to insert data to first index like this list.insertAll(0,data) but when I do that my list is automatically scrolled but when I use list.add() it is not automatically scrolling.

I have tried most of the methods and scroll physics even I have made one custom scroll physics which remains scroll when inserting data but nothing works in my project I have created on demo project and it is working in that.

2

Answers


  1. Can you try the following?

    Add this Scrollcontroller to your widget.

    final ScrollController _scrollController = ScrollController();
    

    Add the controller to your ListView (or whatever you use)

    return ListView.builder(
                      controller: _scrollController, //add this line
    ...
    

    Lets first try with a button.

    IconButton(
                    onPressed: () {
                      if (_scrollController.hasClients) {
                        final position =
                            _scrollController.position.maxScrollExtent; //or minScrollExtent
                        _scrollController.animateTo(
                          position,
                          duration: Duration(seconds: 1),
                          curve: Curves.easeInOut,
                        );
                      }
                    },
                    icon: Icon(Icons.arrow_downward))
    
    Login or Signup to reply.
  2. Just try to add Controller with "keepScrollOffset" property see if it works.
    Example

    ScrollController controller = ScrollController(keepScrollOffset: true);
    
    ListView.builder(
      controller: controller
      reverse: true, 
      itemBuilder: .....
    );
    

    Use this statement while adding data to list

    controller .jumpTo(0.0); 
    list.insertAll(0, newData);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search