Home > OS >  Go to the end of the list as soon as you create it, Flutter
Go to the end of the list as soon as you create it, Flutter

Time:04-30

I read many great answers about a ScrollController in the ListView. Then when a button is clicked we do scrollController.jumpTo(scrollController.position.maxScrollExtent);

My question is, can we go to the bottom of a List, but at the creation of it ?

return ListView.builder(
        itemCount: data.length,
        shrinkWrap: true,
        // CAN WE DO SOMETHING HERE FOR EXAMPLE ?
        physics: const ScrollPhysics(),
        itemBuilder: (context, index)
        {
          return Item(data[index]);
        }
    );

(For me, it's a chat so the reverse option doesn't look good)

CodePudding user response:

If, as you said, you are using this list for a chat log, then the reverse options does work for you. You just have to also invert your chat log data:

data.reversed.toList()

There is not direct option on a ListView to go to the end of the list.


For a more complete example from one of my applications:

class MessageList extends StatelessWidget {
  const MessageList(this.displayMessages);

  final List<Map> displayMessages;

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: ListView(
          reverse: true,
          children: displayMessages.map((message) {
            return ChatMessage(message);
          }).toList(),
        ),
      ),
    );
  }
}
displayMessages = List<Map>.from(clientMessages.data).reversed.toList();
  • Related