ListView vs SingleChildScrollView

317 views Asked by At

Well, I am making a chat application I am confused about which component should I opt for rendering chats, between SingleChildScrollView and ListView, I read a couple of articles, and they suggest

SingleChildScrollView, Since it's a chat application so each chat bubble will not be the same therefore ListView will not be performant.

But, In SingleChildScrollView all items inside the column are rendered at once even if they are not inside the ViewPort or in other words visible. Doing so should not be memory efficient.

So which one should I Opt?

1

There are 1 answers

0
Marcel Dz On

You should go for the ListView.builder() since it creates items on demand.

Example:

child: ListView.builder(
  reverse: true,
  scrollDirection: Axis.vertical,
  padding: const EdgeInsets.only(top: 35, bottom: 25),
  itemCount: messages.length,
  controller: controller,
  itemBuilder: (context, index) {
    return ChatBubble(
      time: messages[index].time,
      chatPartner: chatPartner,
      message: messages[index].message,
      isMe: messages[index].sentBy != cId ? true : false,
    );
  },
),

docs: https://api.flutter.dev/flutter/widgets/ListView/ListView.builder.html