Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I focus the last item of a listView in flutter

Tags:

flutter

I'm trying to make my listview always focus on the last element in a chat, but I don't know how to do it, I appreciate if someone can help me

Widget ChatMessageList(){
    return StreamBuilder(
      stream: chatMessageStream,
      builder: (context, snapshot){
        return snapshot.hasData ? ListView.builder(
          controller: _scrollController,
          itemCount: snapshot.data.documents.length,
          itemBuilder: (context, index){
            return MessageTile(snapshot.data.documents[index].data()['message'],
                snapshot.data.documents[index].data()['sendBy'] == userSnapshop.uid);
          }
        ) : Container();
      },
    );
  }
like image 888
Gustavo Barrios Avatar asked Sep 17 '25 14:09

Gustavo Barrios


1 Answers

List view can be reverse by wrapping another scrollable widget.

so you just need to wrap your ListView by SingleChildScrollView and change the reading direction by revers property.

SingleChildScrollView(
        reverse: true,
        child: ListView.builder(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          itemCount: 100,
          itemBuilder: (context, index) => Text(
            index.toString(),
          ),
        ),
      )
like image 115
ajay Avatar answered Sep 20 '25 08:09

ajay